Debugging 'not a valid MySQL result resource' errors?



I have devel installed and such, but occasionally I get errors like:

warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\d6\includes\database.mysql.inc on line 160.

Now in other applications, the line 160 would tell me exactly what query is the problem, but with the db abstraction layer I get no clues... what is the best way for me to track down the naughty query?

Thanks.
KingMoore



Go to the devel admin page an enable stack back trace on error.

nevel

When you have a 'not valid resource' error, it always means one thing: The variable you are trying to perform an action on, usually db_fetch_array() or db_fetch_object(), does not have a proper result from the mysql query. The reason for this one of the following:
1) You had an error in your query syntax, causing the query to fail
2) You have a query that you didn't actually send to the database

For number 1, this often happens when you are trying to pass a variable to your query, and it's empty. For example:

<?php
$element;
db_query('SELECT * FROM {something} WHERE variable="%s"', $element);
?>

In this case, $element is empty, so the query will fail, even though it looks like it's formed properly.

For example with number 2, you may have done this:

<?php
$query = 'SELECT * FROM {something}';
while($item = db_fetch_array($query))
{
  // do something
}
?>

While it should have been:

<?php
$query = 'SELECT * FROM {something}';
$result = db_query($query);
while($item = db_fetch_array($result))
{
  // do something
}
?>

I don't think there are many other reasons why this would be happening.

thanks guys.

KingMoore