-4

此代码工作正常!

$con=mysqli_connect("localhost","root","","laboratory");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM test");

while($row = mysqli_fetch_array($result))
  {
  echo $row['name'];
  echo "<br>";
  }

mysqli_close($con);

但是当我从 mysqli_connect 中删除 database_name 时,我会使用 mysql_select_db,出现以下错误“警告:mysql_select_db() 期望参数 2 是资源,给定的对象”

修改后的代码:

$con=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$db_selected = mysql_select_db("laboratory", $con);

if (!$db_selected)
  {
  die ("Can\'t use laboratory : " . mysql_error());
  }

$result = mysqli_query($con,"SELECT * FROM test");

while($row = mysqli_fetch_array($result))
  {
  echo $row['name'];
  echo "<br>";
  }

mysqli_close($con);
4

3 回答 3

4

替换您的代码:

$db_selected = mysqli_select_db("laboratory", $con);代替

$db_selected = mysql_select_db("laboratory", $con);
于 2013-09-28T09:14:16.433 回答
2

请不要混合mysqlimysql因为它们是不同的模块。

在您使用的第二个代码块中mysql_select_dbmysql_error第一个代码块需要mysql连接,而不是mysqli连接。

于 2013-09-28T09:13:55.907 回答
-1

参数的顺序已更改为:

mysql_select_db($Database, $Connection);

至:

mysqli_select_db($Connection, $Database);
于 2019-05-05T14:53:25.270 回答