<?php
/
* 预处理更新操作
*/
//1.连接数据库
require 'mysqli_connect.php';
//2,sql语句
$sql = "update ignore staff set salary=? where staff_id = ?;";
//3.创建stmt对象
$stmt = mysqli_stmt_init($db);
//4用stmt对象检测当前sql语句是否正确
if(mysqli_stmt_prepare($stmt,$sql)){
mysqli_stmt_bind_param($stmt,'ii',$salary,$staff_id);
$salary = 2800; $staff_id=17;
if(mysqli_stmt_execute($stmt)){
if(mysqli_stmt_affected_rows($stmt)){
echo "更新成功,主键是:".mysqli_stmt_insert_id($stmt);
}else{
echo '没有更新数据';
}
}else{
exit(mysqli_stmt_errno($stmt).":".mysqli_stmt_error($stmt));
}
}else{
exit(mysqli_stmt_errno($stmt).":".mysqli_stmt_error($stmt));
}
//5注销stmt对象
mysqli_stmt_close($stmt);
//6关闭连接
mysqli_close($db);点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号