php mysql_select_db() 期望参数 2 是资源,给定的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16002342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:21:08  来源:igfitidea点击:

mysql_select_db() expects parameter 2 to be resource, object given

phpmysql

提问by Merna

I'm new in using PHP. I made something simple to connect to MySQL and select a database:

我是使用 PHP 的新手。我做了一些简单的事情来连接到 MySQL 并选择一个数据库:

$conn = mysqli_connect($db_host, $db_admin, $db_pass) or die(mysql_error());
// these variables are previously declared and initialized 

$selected_db = mysql_select_db($db_name, $conn) or die(mysql_error());

When I tested it, I got a successfully-established connection and the following warning:

当我测试它时,我得到了一个成功建立的连接和以下警告:

mysql_select_db() expects parameter 2 to be resource, object given 

Why did this happen? How can I fix it?

为什么会这样?我该如何解决?

回答by Fabio

You are using both mysqliand mysqlsimply change

您正在使用两者mysqlimysql只需更改

mysql_select_db()

mysql_select_db()

With

mysqli_select_db

Referencehttp://php.net/manual/en/mysqli.select-db.php

参考http://php.net/manual/en/mysqli.select-db.php

updated

updated

When you use mysql_select_dbyou are supposed to use mysqlapi and so you have to exatibilish connection to database with mysql sintax mysql_connectReference

当你使用mysql_select_db你应该使用mysql的API,所以你必须exatibilish连接到数据库与MySQL sintaxmysql_connect参考

Mysql is now deprecated so it's correct either to use mysqlior PDO

Mysql 现在已被弃用,因此使用mysqli或使用它都是正确的PDO

回答by Chris Forrence

In addition to using mysqli_* consistently (as mentioned in Fabio's answer), there is an additional problem (and a suggestion):

除了始终如一地使用 mysqli_* (如Fabio 的回答中提到),还有一个额外的问题(和一个建议):

  • While the parameter order in mysql_select_databaseare database name, connection, the order of parameters in mysqli_select_dbare connection, database name.

    mysqli_select_db($conn, $db_name);
    
  • As a suggestion, mysqli_connectincludes an optional fourth parameter to connect to a particular database. This would allow you to avoid calling mysql_select_dbaltogether.

    $conn = mysqli_connect($db_host, $db_admin, $db_pass, $db_name)
        or die(mysqli_connect_error());
    
  • 其中参数顺序mysql_select_database是数据库名称、连接,参数顺序mysqli_select_db是连接、数据库名称。

    mysqli_select_db($conn, $db_name);
    
  • 作为建议,mysqli_connect包括可选的第四个参数以连接到特定数据库。这将使您mysql_select_db完全避免调用。

    $conn = mysqli_connect($db_host, $db_admin, $db_pass, $db_name)
        or die(mysqli_connect_error());
    

回答by pratyay

You have to change mysql_select_dbto mysqli_select_dbas pointed out by Fabiobut you'll get an error

你必须改变mysql_select_dbmysqli_select_db如指出的Fabio,但你会得到一个错误

mysqli_select_db() expects parameter 1 to be mysqli, string given

For someone experiencing this, reverse the order of parameters, like for in this case give

对于遇到这种情况的人,请颠倒参数的顺序,例如在这种情况下给出

$selected_db = mysqli_select_db($conn, $db_name)