Correction status:Uncorrected
Teacher's comments:
MySQLi面向对象实现更新操作
1.连接数据库服务器,建立mysqli对象
<?php //1.连接参数 $db_host = 'localhost'; $db_user = 'root'; $db_pass = 'root'; $db_name = 'php'; $db_charset = 'utf8'; //2.连接数据库服务器,并返回mysqli对象 $mysqli = @new mysqli($db_host,$db_user,$db_pass); //3.检测是否连接成功 if ($mysqli->connect_errno) { exit('连接错误'.$mysqli->connect_errno.':'.$mysqli->connect_error); } //4.设置默认数据库 $mysqli->select_db($db_name); //5.设置客户端默认字符编码集 $mysqli->set_charset($db_charset);
点击 "运行实例" 按钮查看在线实例
2.mysqli面向对象更新操作 将 kc_id =18 的记录 单价(dj)更新为5.5
<?php /** * mysqli面向对象更新操作 */ //1.连接数据库 require 'mysqli_connect.php'; //2.准备SQL语句 $sql = "UPDATE kc SET dj = ? WHERE kc_id = ?;"; //3.创建STMT预处理对象 $stmt = $mysqli->stmt_init(); //4.检测预处理SQL语句 if ($stmt->prepare($sql)) { //绑定参数到预处理SQL语句 $stmt->bind_param('di',$dj,$kc_id); $dj = 5.5; $kc_id = 18; //执行预处理语句 $stmt->execute(); //检测运行结果 if ($stmt->affected_rows > 0 ){ echo '成功更新'.$stmt->affected_rows.'条记录'; } else { echo '没有记录被更新'; } //5.注销stmt对象 $stmt->close(); } else { //返回错误信息 exit($stmt->errno.':'.$stmt->error); } //6. 关闭数据库连接 $mysqli->close();
点击 "运行实例" 按钮查看在线实例