21

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?

1
  • After "fixing" an issue like this on an old app, I don't think showing some code would help. Because of the way the PHP mysql library "works," it can be difficult to see problems. Especially in spaghetti! Jan 28, 2016 at 19:38

4 Answers 4

31

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.

$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 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);

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

Using $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 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.

3
  • A-ha! Most pages on my site included multiple files that included the MySQL connection file, so conflicts emerged as described in your post. I solved it by putting the MySQL connection in the header and the connection closing statement in the footer.
    – Pieter
    May 21, 2010 at 19:49
  • Thanks for the mention of the $new_link parameter. I had a daemon running that was using a shared mysql connection across children (which was breaking stuff) so I needed to break down my connection and re-establish it in each child process instead of sharing the connection across children. Turns out it was still reusing connection ids that had been destroyed so I was getting the not a valid MySQL-Link resource error. Aug 31, 2011 at 19:13
  • @gapple As you mentioned mysql_close() was the problem in my case as it was above the query . Without knowing that i spent a lot of time googling for the cause and your post saved me . Thanks a lot mate
    – Raja Gopal
    Apr 6, 2016 at 7:24
6

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.

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);
}
4

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!

0
1

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.