Correction status:Uncorrected
Teacher's comments:
多写多练多想,数据库增删改查重点放在后面的PDO技术上
数据库连接代码:
<?php /** * 连接数据库 */ //1.连接参数 $db_host = '127.0.0.1'; $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); } //echo '<h1>连接成功</h1>'; //4.设置默认数据库 $mysqli->select_db($db_name); //5.设置客户端默认字符编码集 $mysqli->set_charset($db_charset);
点击 "运行实例" 按钮查看在线实例
更新操作代码:
<?php /** * 新增操作 */ //1.连接数据库 require 'mysql_connect.php'; //2.准备SQL语句 $sql = "UPDATE user SET email=? WHERE user_id=?;"; //3.创建STMT预处理对象 $stmt = $mysqli->stmt_init(); //4.检测预处理SQL语句 if ($stmt->prepare($sql)) { //绑定参数到预处理SQL语句 $stmt->bind_param('si',$email,$id); //设置参数 $id = 2; $email = 'tutu@qq.com'; //执行预处理语句 $stmt->execute(); if ($stmt->affected_rows > 0 ){ echo '<br>成功更新了'.$stmt->affected_rows.'条记录'; } else { echo '<br>没有记录被更新'; } //5.注销stmt对象 $stmt->close(); } else { exit($stmt->errno.':'.$stmt->error); } //6. 关闭数据库连接 $mysqli->close();
点击 "运行实例" 按钮查看在线实例