警告:mysql_query():3不是有效的MySQL-Link资源 [英] Warning: mysql_query(): 3 is not a valid MySQL-Link resource

查看:66
本文介绍了警告:mysql_query():3不是有效的MySQL-Link资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个奇怪的错误,我不知道它是从哪里来的:

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 (...)

3是怎么回事?我不明白有人亲自经历过此错误吗?

What's up with the 3? I don't get it. Has anyone experienced this error themselves?

推荐答案

PHP使用资源作为特殊变量来保存指向外部对象(例如文件和数据库连接)的链接.每个资源都有一个整数ID. (文档)

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)

如果数据库连接失败,您可能会收到指定的变量不是有效的MySQL-Link资源"错误,如Dan Breen所述,因为应该保存该资源的变量为空.

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.

$link = mysql_connect('localsoth','baduser','badpass'); // failed connection
$result = mysql_query("SELECT 1", $link); // throws error

由于在错误消息中获取了特定的资源ID,因此数据库连接可能由于某种原因意外关闭.您的程序仍然具有带有资源ID的变量,但是外部对象不再存在. 可能是由于在mysql_query调用之前某个地方的mysql_close()调用,或者是关闭了连接的外部数据库错误.

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 may be due to a mysql_close() call somewhere before the call to mysql_query, or an external database error that closed the connection.

$link = mysql_connect();
mysql_close($link);
// $link may still contain a resource identifier, but the external object is gone
mysql_query("SELECT 1", $link);


重用连接

mysql扩展和mysql_connect()的问题是,默认情况下,如果在连续调用中传递相同的参数,它将重新使用现有连接,而不是创建新连接(


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 true to the $new_link parameter.
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.

$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

使用$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



顺便说一句,我建议使用
MySQLi 扩展名或 http://www.php.net/manual/en/mysqli.overview .php 了解有关这三个界面之间差异的一些详细信息.



As an aside, I would recommend using the MySQLi extension or PDO instead, 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.php for some details on the differences between the three interfaces.

这篇关于警告:mysql_query():3不是有效的MySQL-Link资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆