php 警告:mysql_query():3 不是有效的 MySQL-Link 资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2851420/
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
Warning: mysql_query(): 3 is not a valid MySQL-Link resource
提问by Pieter
I got this odd error and I can't figure out where it came from:
我遇到了这个奇怪的错误,我不知道它来自哪里:
Warning: mysql_query(): 3 is not a valid MySQL-Link resource in (...)
Warning: mysql_query(): 3 is not a valid MySQL-Link resource in (...)
What's up with the 3? I don't get it. Has anyone experienced this error themselves?
3号怎么了?我不明白。有没有人自己经历过这个错误?
回答by gapple
PHP uses resources as a special variable to hold links to external objects, such as files and database connections. Each resource is given an integer id. (Documentation)
PHP 使用资源作为特殊变量来保存到外部对象的链接,例如文件和数据库连接。每个资源都有一个整数 id。(文档)
Failed Connections
失败的连接
If the database connection fails you'll likely get a "Specified variable is not a valid MySQL-Link resource" error, as Dan Breen mentioned, since the variable that is supposed to hold the resource is null.
如果数据库连接失败,您可能会收到“指定的变量不是有效的 MySQL-Link 资源”错误,正如 Dan Breen 所提到的,因为应该保存资源的变量为空。
$link = mysql_connect('localsoth','baduser','badpass'); // failed connection
$result = mysql_query("SELECT 1", $link); // throws error
Since you're getting a specific resource ID in the error message, the database connection likely closed unexpectedly for some reason. Your program still has a variable with a resource ID, but the external object no longer exists. This maybe due to a mysql_close()call somewhere before the call to mysql_query, or an external database error that closed the connection.
由于您在错误消息中获得了特定的资源 ID,因此数据库连接可能由于某种原因意外关闭。您的程序仍然有一个带有资源 ID 的变量,但外部对象不再存在。这可能是由于在mysql_close()调用 之前某处调用了mysql_query,或者关闭了连接的外部数据库错误。
$link = mysql_connect();
mysql_close($link);
// $link may still contain a resource identifier, but the external object is gone
mysql_query("SELECT 1", $link);
Reusing Connections
重用连接
An issue with the mysql extension and mysql_connect()is that by default if you pass the same parameters in successive calls, it will re-use the existing connection rather than create a new one (Documentation). This can be fixed by passing trueto the $new_linkparameter.
I encountered this myself on a test system where the data from two separate databases in production were combined on to one test server, and in testing the mysql_xxx()function calls walked over each other and broke the system.
mysql 扩展的一个问题mysql_connect()是,默认情况下,如果您在连续调用中传递相同的参数,它将重新使用现有连接而不是创建一个新连接(文档)。这可以通过传递true给$new_link参数来修复。
我自己在一个测试系统上遇到了这个问题,其中来自生产中两个独立数据库的数据被合并到一个测试服务器上,在测试中,mysql_xxx()函数调用相互交叉并破坏了系统。
$link1 = mysql_connect('localhost','user','pass'); // resource id 1 is given
$link2 = mysql_connect('localhost','user','pass'); // resource id 1 is given again
mysql_close($link2); // the connection at resource id 1 is closed
mysql_query("SELECT 1", $link1); // will fail, since the connection was closed
Using $new_link:
使用$new_link:
$link1 = mysql_connect('localhost','user','pass'); // resource id 1 is given
$link2 = mysql_connect('localhost','user','pass', true); // resource id 2 is given
mysql_close($link2); // the connection at resource id 2 is closed
mysql_query("SELECT 1", $link1); // the connection at resource id 1 is still open
Edit:
As an aside, I would recommend using the MySQLiextension or PDOinstead, if possible. The MySQL extension is getting pretty old, and can't take advantage of any features past MySQL version 4.1.3. Look at http://www.php.net/manual/en/mysqli.overview.phpfor some details on the differences between the three interfaces.
编辑:顺便说一句,如果可能
,我建议改用MySQLi扩展或PDO。MySQL 扩展已经很老了,无法利用 MySQL 4.1.3 版之后的任何功能。查看http://www.php.net/manual/en/mysqli.overview.php了解有关三个接口之间差异的一些详细信息。
回答by Tarilonte
I also had this problem. In examining my code I found I had an include of a script that closed the connection, so when php tried to close it again we got the error.
我也有这个问题。在检查我的代码时,我发现我包含一个关闭连接的脚本,所以当 php 尝试再次关闭它时,我们得到了错误。
To solve this, just check if the connection is open before trying to close it:
要解决这个问题,只需在尝试关闭连接之前检查连接是否打开:
instead of:
代替:
mysql_close($con);
Do this:
做这个:
if( gettype($con) == "resource") {
mysql_close($con);
}
回答by ben
I had this error just a minute ago, it was because i was including a my database connection file which had a close connection function at the bottom. Get rid of your close connection and your be fine!
一分钟前我遇到了这个错误,这是因为我包含了一个我的数据库连接文件,该文件在底部有一个关闭连接功能。摆脱你的亲密关系,你会没事的!
回答by Dan Breen
It sounds like you might be getting an error while trying to connect to the database, and the mysql handle is not actually a valid connection. If you post more code, like how you're connecting to the database, that would be more helpful. Make sure you're checking for errors, too.
听起来您在尝试连接到数据库时可能会遇到错误,而 mysql 句柄实际上不是一个有效的连接。如果您发布更多代码,例如您如何连接到数据库,那会更有帮助。确保您也在检查错误。

