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
mysql_select_db() expects parameter 2 to be resource, object given
提问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 mysqli
and mysql
simply change
您正在使用两者mysqli
,mysql
只需更改
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_db
you are supposed to use mysql
api and so you have to exatibilish connection to database with mysql sintax mysql_connect
Reference
当你使用mysql_select_db
你应该使用mysql
的API,所以你必须exatibilish连接到数据库与MySQL sintaxmysql_connect
参考
Mysql is now deprecated so it's correct either to use mysqli
or 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_database
are database name, connection, the order of parameters inmysqli_select_db
are connection, database name.mysqli_select_db($conn, $db_name);
As a suggestion,
mysqli_connect
includes an optional fourth parameter to connect to a particular database. This would allow you to avoid callingmysql_select_db
altogether.$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_db
to mysqli_select_db
as pointed out by Fabio
but you'll get an error
你必须改变mysql_select_db
到mysqli_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)