实例
<?php require 'mysqli_cofig.php'; //2 调用连接函数返回链接对象 $db =mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die('连接失败'); //判断是否链接成功 //if(mysqli_connect_errno($db)){ // exit('连接失败'.mysqli_connect_error($db)); //} //echo'<h1>连接成功</h1>'; //4选择默认数据库 //mysqli_select_db($db,DB_NAME); //5设置客户端默认字符编码集 mysqli_set_charset($db,DB_CHAR);
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: 金超 * Date: 2018/4/25 * Time: 10:28 */ //1 创建链接参数 define('DB_HOST','127.0.0.1'); define('DB_USER','root'); define('DB_PASS','root'); define('DB_NAME','php'); define('DB_CHAR','UTF8');
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php require 'mysqli_connect.php'; //执行查询 $sql="SELECT*FROM staff;"; if($res=mysqli_query($db,$sql)){ while($res2=mysqli_fetch_array($res,MYSQLI_ASSOC)){ var_export($res2);print '<hr style="color:indianred">'; } }else { exit('查询失败'.mysqli_errno($db).':'.mysql_connect_error($db)); } //释放结果集 mysqli_free_result($res); //关闭数据库 mysqli_close($db);
运行实例 »
点击 "运行实例" 按钮查看在线实例