实例
mysql_config.php: <?php /医院 * Created by PhpStorm. * User: Administrator * Date: 2018/4/24 0024 * Time: 下午 1:08 */ header("content-type:text/html;charset=utf-8"); define("DB_HOST","127.0.0.1"); define("DB_USER","root"); define("DB_PASS","root"); define("DB_NAME","testguest"); define("DB_CHAR","utf8"); mysqli_connect.php: <?php /医院 * Created by PhpStorm. * User: Administrator * Date: 2018/4/24 0024 * Time: 下午 1:11 */ header("content-type:text/html;charset=utf-8"); require_once "mysql_config.php"; $conn = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die("数据库连接失败"); $conn->set_charset(DB_CHAR); mysql_query.php: <?php /医院 * Created by PhpStorm. * User: Administrator * Date: 2018/4/24 0024 * Time: 下午 2:09 */ header("content-type:text/html;charset=utf-8"); require_once "mysqli_connect.php"; $sql = "SELECT * FROM tg_user"; if ($result = mysqli_query($conn,$sql)){ // echo $result->num_rows; while ($res = mysqli_fetch_array($result)){ echo '用户名:'.$res['dg_username'].'密码:'.$res['dg_password'].'<br>'; } $result->close(); } $conn->close(); mysqli_multi_query.php: <?php /医院 * Created by PhpStorm. * User: Administrator * Date: 2018/4/24 0024 * Time: 下午 2:09 */ header("content-type:text/html;charset=utf-8"); require_once "mysqli_connect.php"; $sql = "SELECT * FROM tg_user;"; $sql .="UPDATE tg_user SET dg_username='rootadmin' WHERE dg_id=2"; if ($conn->multi_query($sql)){ do{ if ($result = $conn->store_result()){ while ($row = $result->fetch_row()) { echo "<pre>"; print_r($row); } $result->free(); } if (!$conn->more_results()) { echo "没有更多的了"; return; } }while($conn->next_result()); } $conn->close();
运行实例 »
点击 "运行实例" 按钮查看在线实例