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 (...)
What’s up with the 3? I don’t get it. Has anyone experienced this error themselves?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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)
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.
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 tomysql_query, or an external database error that closed the connection.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 passingtrueto 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.Using
$new_link:Edit:
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.