Debugging 'not a valid MySQL result resource' errors?

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


23 points

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



5 points

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

nevel

Anonymous's picture
Created by Anonymous
42 points

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.

Anonymous's picture
Created by Anonymous
14 points

thanks guys.

KingMoore

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.