Blogger Information
Blog 46
fans 3
comment 1
visits 33390
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
424 MySQL预处理更新与删除
吃不起土的少年的博客
Original
829 people have browsed it

更新uadate

实例

<?php
/**
 * 预处理的简化版本:以新为例
 * 一次新增多条进行介绍
 */

//1.连接数据库
require 'mysqli_connect.php';

//2.准备SQL语句
$sql = "UPDATE staff set salary =? WHERE sex=?;";

//3.创建stmt对象
$stmt = mysqli_stmt_init($db);

//4.检测SQL语句
if (mysqli_stmt_prepare($stmt, $sql)) {

    /* 参数绑定 */
    mysqli_stmt_bind_param($stmt, "ii", $salary,$sex);
    $sex= 1;
    $salary = 8888;

    /* 执行SQL语句 */
    mysqli_stmt_execute($stmt);
    echo '<br>更新了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt);

    //再次添加新数据,只需要给新变量并执行一下就可以了
//    $sex= 0;
//    $salary = 6666;
//
//    /* 执行SQL语句 */
//    mysqli_stmt_execute($stmt);
//    echo '<br>更新了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt);




} else {
    exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
}
/* 注销stmt对象 */
mysqli_stmt_close($stmt);

/* 关闭数据库连接 */
mysqli_close($db);

运行实例 »

点击 "运行实例" 按钮查看在线实例

微信图片_20180425213113.png

微信图片_20180425213130.png

微信图片_20180425213133.png

删除 delete

实例

<?php
/**
 * 预处理的简化版本:以新为例
 * 一次新增多条进行介绍
 */

//1.连接数据库
require 'mysqli_connect.php';

//2.准备SQL语句
$sql = "DELETE FROM staff  WHERE salary=?;";

//3.创建stmt对象
$stmt = mysqli_stmt_init($db);

//4.检测SQL语句
if (mysqli_stmt_prepare($stmt, $sql)) {

    /* 参数绑定 */
    mysqli_stmt_bind_param($stmt, "i", $salary);

    $salary = 6000;

    /* 执行SQL语句 */
    mysqli_stmt_execute($stmt);
    echo '<br>删除了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt);

    //再次添加新数据,只需要给新变量并执行一下就可以了
//    $sex= 0;
//    $salary = 6666;
//
//    /* 执行SQL语句 */
//    mysqli_stmt_execute($stmt);
//    echo '<br>更新了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt);




} else {
    exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
}
/* 注销stmt对象 */
mysqli_stmt_close($stmt);

/* 关闭数据库连接 */
mysqli_close($db);

运行实例 »

点击 "运行实例" 按钮查看在线实例

微信图片_20180425213136.png

微信图片_20180425213140.png

微信图片_20180425213142.png

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post