Correction status:qualified
Teacher's comments:
<?php /医院 * Created by PhpStorm. * User: Administrator * Date: 2018/4/24 * Time: 7:42 */ define ('DB_HOST', 'localhost'); define ('DB_USER', 'root'); define ('DB_PASS', 'root'); define ('DB_NAME', 'php'); define ('DB_CHAR', 'utf8');
点击 "运行实例" 按钮查看在线实例
<?php /医院 mysqli_connect() */ //1、连接数据库 require 不是函数 require 'mysqli_config.php'; //2、执行查询操作 $con = mysqli_connect(DB_HOST,DB_USER,DB_PASS); //3、检查连接 if (!$con){ die("连接错误:".mysqli_connect_error()); } echo "连接成功";
点击 "运行实例" 按钮查看在线实例
mysqli_connect() 函数打开一个到 MySQL 服务器的新的连接。
语法:mysqli_connect(host,username,password,dbname,port,socket);
注意: define的后面一定要带有空格 例 define ('DB_HOST', 'localhost');
<?php /医院 * Created by PhpStorm. * User: Administrator * Date: 2018/4/24 * Time: 8:17 */ //1、连接数据库 require 'mysqli_config.php'; //2、执行查询 $con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME); $sql="select * from user"; $res = mysqli_query($con,$sql); //$rows = mysqli_fetch_assoc($res); $rows = mysqli_fetch_array($res); //用结果组成数组 var_export($rows); //3、释放结果集(仅针对select) mysqli_free_result($res); //4.关闭数据库连接 mysqli_close($con);
点击 "运行实例" 按钮查看在线实例
mysqli_query() 函数执行某个针对数据库的查询
语法 : mysqli_query(connection,query,resultmode);