1.预处理更新
实例
<?php require 'mysqli_con.php'; $sql = "UPDATE staff SET salary=10000 WHERE age=?;"; $stmt = mysqli_stmt_init($db); //创建对象 if(mysqli_stmt_prepare($stmt,$sql)){ //检测语句 mysqli_stmt_bind_param($stmt,"i",$age); //绑定参数 $age = 43; mysqli_stmt_execute($stmt); //执行语句 echo 'new update:'.mysqli_stmt_affected_rows($stmt); } else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); mysqli_close($db);
运行实例 »
点击 "运行实例" 按钮查看在线实例
2.预处理删除
实例
<?php require 'mysqli_con.php'; $sql = "DELETE FROM staff WHERE staff_id=?"; $stmt = mysqli_stmt_init($db); //创建对象 if(mysqli_stmt_prepare($stmt,$sql)){ //检测语句 mysqli_stmt_bind_param($stmt,"i",$staff_id); //绑定参数 $staff_id = 7; mysqli_stmt_execute($stmt); //执行语句 echo 'new delete:'.mysqli_stmt_affected_rows($stmt); } else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); mysqli_close($db);
运行实例 »
点击 "运行实例" 按钮查看在线实例