View Full Version : supplied argument is not a valid MySQL result resource
oenkitt
02-19-2010, 03:23 AM
I keep getting the "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" error with the following code:
$query = "SELECT * FROM table WHERE `field1` is not null AND `field2` is not null AND `field3` is null";
$result = mysql_query($query) or die(mysql_error());
while ( $row = mysql_fetch_array($result) ) {
//stuff here
}
It actually seems to work just fine, as it still completes the intended operation, and mysql_error() doesn't return anything.
Can anyone tell me what I'm doing wrong? I've been searching for a while now and haven't found anything relevant. Sorry if it's really obvious, I'm fairly new to PHP and MySQL.
shadmego
02-19-2010, 08:55 AM
It difficult to tell you where the error is in your code. The "not a valid resource" error typically indicates a SQL statement error.
What you might do to try and further troubleshoot is to break your SQL statement into smaller chunks and count the number of results returned.
$query = "SELECT * FROM table WHERE field1 IS NOT NULL AND field2 IS NOT NULL and field3 IS NULL";
$result = mysql_query($query) or die("Error: ".mysql_error());
echo mysql_num_rows($result);
Keep adding to it until you get a positive result from your original code on the mysql_fetch_array().
Just so you know, you should pay close attention to table names and your NULL values. The following is good place to read about NULL values, which are not the same thing as the string "NULL".
Basically, NULL is a lack of a value and a state. You would have to check your database tables and verify that your NULLs are really NULL and not "NULL" or empty.
I think that might be where you problem is.
http://lists.mysql.com/mysql/206469
albert
02-19-2010, 01:01 PM
$result might be null?
shadmego
02-19-2010, 01:08 PM
It might be, but the error seems to indicate there is either an incorrect value from the SQL statement (IS NOT NULL .... IS NULL ....) or there is a misnamed field name (field1, field2, etc).
oenkitt
02-19-2010, 01:55 PM
But since the operation still works, (as in, it still finds the rows that I'm trying to select), doesn't that mean that all of my values are correct? Otherwise it wouldn't find anything, or it would find the wrong rows. And yes, I realize the difference between NULL and the string "NULL" and it's correct in my database too, I believe.
I did try breaking the query into chunks, and it still finds everything that it's supposed to find.. It really seems to be working just fine, but it throws out that error at the end anyways. :/
albert
02-19-2010, 02:14 PM
Not the password of course.
Send the Create SQL & the whole php code.
oenkitt
02-22-2010, 01:26 PM
Well I was able to fix it by putting part of the script into it's own function. I'm not sure how that fixed it, but I'm no longer getting the error. :)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.