总结:
基本不步骤如下:
1连接数据库
2准备多条查询sql语句
3创建stmt对象
4执行查询并检测
4—1绑定参数
4—2执行sql语句
实例
<?php //连接数据库 require 'mysqli_connect.php'; //准备多条查询sql语句 // $sql = 'UPDATE staff SET salary=? WHERE staff id=?;'; $sql = 'DELETE FROM staff WHERE staff id=?;'; //创建stmt对象 $stmt = mysqli_stmt_init($db); //执行查询并检测 if (musqli_stmt_prepae($stmt,$sql)) { //绑定参数 mysqli_stmt_bind_param($stmt,$id); $id = 3; //执行sql语句 mysqli_stmt_execute($stmt); echo '<br>删除'mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt); } else { exit (mysqli_stmt_errno($stmt).':'.misqli_stmt_error($stmt)); } //注销对象stmt mysqli_stmt_close($stmt); //关闭数据库连接 mysqli_close($db); <?php //连接数据库 require 'mysqli_connect.php'; //准备多条查询sql语句 $sql = 'UPDATE staff SET salary=? WHERE staff id=?;'; // $sql = 'DELETE FROM staff WHERE staff id=?;'; //创建stmt对象 $stmt = mysqli_stmt_init($db); //执行查询并检测 if (musqli_stmt_prepae($stmt,$sql)) { //绑定参数 mysqli_stmt_bind_param($stmt,$salary,$id); $id = 7; $salary = 200; //执行sql语句 mysqli_stmt_execute($stmt); echo '<br>更新了'mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt); } else { exit (mysqli_stmt_errno($stmt).':'.misqli_stmt_error($stmt)); } //注销对象stmt mysqli_stmt_close($stmt); //关闭数据库连接 mysqli_close($db);
运行实例 »
点击 "运行实例" 按钮查看在线实例
5注销对象stmt
6关闭数据库连接