使用 mysqli_connect 和 mysql_select_db[英] use mysqli_connect and mysql_select_db

本文是小编为大家收集整理的关于使用 mysqli_connect 和 mysql_select_db的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

此代码正常工作!

$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中删除数据库_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);

推荐答案

请,不要混合mysqli和mysql,因为它们是不同的模块.

在您的第二个代码块中,您正在使用mysql_select_db和mysql_error,第一个需要mysql连接,而不是mysqli连接.

其他推荐答案

替换您的代码:

$db_selected = mysqli_select_db("laboratory", $con);而不是

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

其他推荐答案

参数的顺序已从:

更改为:

mysql_select_db($Database, $Connection);

to:

mysqli_select_db($Connection, $Database);

本文地址:https://www.itbaoku.cn/post/597375.html

问题描述

This code works correctly!

$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);

But when I remove database_name from mysqli_connect I would use the mysql_select_db, the following error occurs "Warning: mysql_select_db() expects parameter 2 to be resource, object given in"

Code after change:

$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);

推荐答案

Please, don't mix mysqli and mysql as they are different modules.

In your second code block you are using mysql_select_db and mysql_error, the first one requires mysql connection, not mysqli connection.

其他推荐答案

Replace Your Code:

$db_selected = mysqli_select_db("laboratory", $con); instead of

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

其他推荐答案

The order of the parameters has been changed from:

mysql_select_db($Database, $Connection);

to:

mysqli_select_db($Connection, $Database);