Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_fetch_array() question

Status
Not open for further replies.

GiffordS

Programmer
May 31, 2001
194
0
0
CA
Ok, I have a real riddle this time. I have a query that, when run, throws the following error:

mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Not unusual, I know. But... here is where the riddle comes in. There is no problem with the query itself. This is a standard query:

$a="select * from stuff";
$b=mysql_query($a) or die(mysql_error());
while($c=mysql_fetch_array($b, MYSQL_ASSOC))

The query is run on several other pages of this site and works perfectly. I even went so far as to copy and paste it from a working page into the current page... it still threw the error. There is literally nothing on this page before the query that would cause the error.

I know the issue isn't with the db connection or the query itself because if I change the mysql_fetch_array() statement to something like

$c=mysql_num_rows($b);

it returns the exact number of rows in that table. So the query itself does not return false. It returns data. I'm really scratching my head here and would love to here any ideas that are out there.
 
Hi GiffordS,

try this:
$a="select * from stuff";
$b=mysql_query($a) or die(mysql_error());
while($c=mysql_fetch_assoc($b))

cu berges
 
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

This just makes no sense. It's acting like the query is returning false, but as I said before, when I swap num_rows() for fetch_array() it returns the number of rows in the table. Besides, this is EXACTLY the same query that is being run on about ten other pages of this site, and all the rest work perfectly.
 
I assume the code you posted isn't the exact code you're using. Please post the exact code. And please put it between [ignore]
Code:
[/ignore]
tags.

Ken
 
anything else in that page or being included that would overwrite $b?

try renaming the vars:
Code:
$sql = "select * from stuff";
$rs = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($rs))

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
ken,

that is the exact code.

jemminger,

That's a great point and one I should have mentioned in my original post. I did check that before posting. For anyone else reading this thread just to learn, that really is a good point.

As for the query, I simply re-wrote the page beginning with the query from one of the other working pages and it's fine. I have absolutely no idea what the difference is. They look the same to me, but it's all working now. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top