Warning: MySQL_Query(): 3 Is Not a Valid MySQL-Link Resource

Warning: mysql_query(): 3 is not a valid MySQL-Link resource

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.

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

Based on the comments, it sounds like the problem is caused by using require_once() inside a function.

One of two thing is happening. Either:

  1. You've already included Connection.php somewhere else, so when you get to the function, it's not actually included.. because of the once part of require_once.

    or...

  2. It is working the first time you call the function, but the second time you call it, the file has already been included and does not get included again.

The problem is that when the file is first included (assuming that's from within this function), the $connection variable is created in the function scope, and like any other function variable, is gone at the end of the function. When you call the function a second time, the include doesn't happen because you're using require_once.

You could probably fix this by calling require() instead of require_once(), but that will end up reconnecting to the database every time you call the function - which is a lot of unnecessary overhead. It's much cleaner to just move the include outside of the function, and either pass the connection into the function, or use it as a global variable.

That would look like this:

require_once('Connection.php');

function getResult() {
global $connection;

$findQuery = "SELECT * FROM `Keys` WHERE `ID` = '$gID'";
$findResult = mysql_query($findQuery, $connection) or die(mysql_error());
$resultRow = mysql_fetch_assoc($findResult) or die(mysql_error());
}

All that being said, there's 2 major problems with this code.

  1. You're using the mysql_* functions which are deprecated and will soon be removed from new versions of PHP. See this question for more details: Why shouldn't I use mysql_* functions in PHP?

    It's not actually that hard to switch to something like the mysqli_* functions instead - there's a non-object set of functions that are almost identical to what you're using now.

  2. You're including a variable in your query without properly escaping it. At the very least you should be calling mysql_real_escape_string() (or mysqli_real_escape_string()), but a better solution is to look into prepared statements. You can find more information on prepared statements here: How can I prevent SQL injection in PHP?

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/vinem/www/batch/batch_stock.php on line 47

You open your connection which is fine, yet right after it you're closing the connection link. So that connection no longer exists.

Remove this,

mysql_close($link);

Edit 1

Your code is prone to SQL injection, you are still using MySQL even though it has been deprecated, you should use either MySQLi or PDO with prepared statements.

mysql_query(): 3 is not a valid MySQL-Link resource?

Feel the power of google.

While writing your question on SO brings you no help, exact error message pasted into google's search box finds you complete solution. In much less time.

mysql_query(): supplied argument is not a valid MySQL-Link resource

Your parameters for mysql_query() are in the wrong order.

This

$result = mysql_query($connection, $query)

should be

$result = mysql_query($query, $connection)

http://php.net/manual/en/function.mysql-query.php

resource mysql_query ( string $query [, resource $link_identifier ] )

Update

When I said use mysql_error(), I meant only if there was an apparent error. Try something like this

if (isset($_POST['accession_number'])) {
$accession_number = $_POST['accession_number'];
$query = sprintf('INSERT INTO `top` (accession_number) VALUES (%d)',
$accession_number);
$result = mysql_query($query);
if (false === $result) {
throw new Exception('Error in query you have, hmmm: ' . mysql_error());
}
// and so on

I highly recommend ditching the MySQL library entirely and moving to PDO. Writing the above code makes me feel dirty.

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource

mysql_select_db() expects the second parameter to be a resource identifier = your connection. The problem is, that you are running this as a function, inside which your connection is not established. You have to start your function with something like this:

function Visit($url)
{
$spyware = mysql_connect(); // set this to connect properly
echo $url;
mysql_select_db($database_spyware, $spyware) || die(mysql_error());
// the rest of your function goes on ...

Warning: mysql_query(): 7 is not a valid MySQL-Link resource

The symptoms suggest that the database connection has been closed or dropped. Look for unwanted mysql_close() calls in your code. Additionally, you can use the following functions to troubleshoot the issue:

  1. is_resource() and get_resource_type() to confirm that $this->connection is a valid data type.
  2. mysql_ping() to find out if the database connection is alive.

If it's a rare issue, log stuff into a file and wait until it happens again.



Related Topics



Leave a reply



Submit