Correction status:qualified
Teacher's comments:
一、数据库操作 //1.创建连接参数 define('DB_HOST', '127.0.0.1'); define('DB_USER', 'root'); define('DB_PASS', 'root'); define('DB_NAME', 'php'); define('DB_CHAR', 'utf8'); //2.调用连接函数mysqli_connect()返回连接对象 $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASS); //3.判断是否连接成功 if(mysqli_connect_errno($dbc)){ exit('连接失败'.mysqli_connect_error($dbc)); } //4.选择默认的数据库 mysqli_select_db($dbc, DB_NAME); //5.设置客户端默认字符编码集 mysqli_set_charset($dbc, DB_CHAR); ///////////////////////////////////////////////////////////// 2、数据库查询的基本步骤mysqli_query() //1.连接数据库 require 'mysqli_connect.php'; //2.编写查询sql语句 $sql = "SELECT * FROM user;"; //3.执行查询并判断结果 if($res = mysqli_query($dbc, $sql)){ while($row = mysqli_fetch_array($res, MYSQLI_ASSOC)){ echo '<pre>'; var_export($row); print '<hr>'; } }else{ exit('查询失败'.mysqli_errno($dbc).':'.mysqli_connect_error($dbc)); } //4.释放结果集 mysqli_free_result($res); //5.关闭连接 mysqli_close($dbc);
点击 "运行实例" 按钮查看在线实例