Correction status:qualified
Teacher's comments:
mysqli_connect.php
<?php //设置连接参数 $DB_HOST='127.0.0.1'; $DB_USER='root'; $DB_PASS='root'; $DB_NAME='mytest'; $DB_CHAR='utf8'; //创建连接对象 $conn=new mysqli($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME); //检测连接 if($conn->connect_errno){ exit('连接错误:'.$conn->connect_errno.':'.$conn->connect_error); }
点击 "运行实例" 按钮查看在线实例
mysql_pre_update.php
<?php header("Content-type: text/html; charset=utf-8"); //连接数据库 require 'MYSQLI_CONNECT.php'; //创建sql语句 $sql="UPDATE user SET name=?,age=?,salary=? WHERE ID=7"; //创建stmt对象 $stmt=$conn->stmt_init(); //检测预处理语句 if($stmt->prepare($sql)){ //绑定预处理数据 $stmt->bind_param('sii',$name,$age,$salary); $name="渣渣辉"; $age=31; $salary=6750; // 执行stmt语句 $stmt->execute(); //判断 if($stmt->affected_rows>0){ echo "影响了".$stmt->affected_rows."条数据"; }else{ echo "没有更新数据!"; } }else{ exit($stmt->errno.":".$stmt->error); } $conn->close();
点击 "运行实例" 按钮查看在线实例
pdo_update.php
<?php header("Content-type: text/html; charset=utf-8"); //创建连接对象 $pdo=new PDO('mysql:dbname=mytest','root','root'); //创建预处理sql语句 $sql="UPDATE `user` SET `name`=:name,`age`=:age,`salary`=:salary WHERE `id`=8;"; //创建stmt对象 $stmt=$pdo->prepare($sql); //绑定数据 $data = ['name'=>'陈小春','age'=>40 ,'salary'=>8140]; $stmt->bindParam(':name',$data['name'],PDO::PARAM_STR); $stmt->bindParam(':age',$data['age'],PDO::PARAM_INT); $stmt->bindParam(':salary',$data['salary'],PDO::PARAM_INT); //提交数据 if($stmt->execute()){ echo "成功更新了".$stmt->rowCount()."条记录"; }else{ echo "error:"; echo $stmt->errorCode(); print_r($stmt->errorInfo()); exit(); } //注销PDO对象 $pdo=null;
点击 "运行实例" 按钮查看在线实例