适应php代码使用mysqli而不是mysql

时间:2017-06-29 07:43:57

标签: php mysql mysqli

我的代码适用于php 5.6但不适用于php 7.0。它很短,所以我想我能够毫无问题地修改它,但我错了。原始脚本如下,然后我尝试使用mysqli。请某位善意的人请告诉我我需要做些什么才能做到正确?

旧代码

<?php
$db_host = "localhost";
$db_name = "database_name";
$db_user = "user_name";
$db_pass = "pass";

$link = mysql_connect($db_host, $db_user, $db_pass) or die("Could not  connect to database as ".$db_user."@".$db_host."!");
mysql_select_db($db_name) or die("Could not select database ".$db_name);
?>

新代码

<?php
$db_host = "localhost";
$db_name = "database_name";
$db_user = "user_name";
$db_pass = "pass";

$link = new mysqli_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB connection');
mysqli_select_db($db_name) or die("Could not select database ".$db_name);
?>

1 个答案:

答案 0 :(得分:1)

$link中添加mysqli_select_db,有关详细信息,请参阅here

mysqli_select_db($link, $db_name)