1

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

I' really stuck on this , I'm gettiing this error: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in "filename"

Here is the code:

 $sql = "SELECT * FROM $tbl_name WHERE....
 $result=mysql_query($sql);
 $row = mysql_fetch_assoc($result);

The wierd thing is that I've used the exact same code before and it worked fine

Any ideas??

Community
  • 1
  • 1
Niall Paterson
  • 3,580
  • 3
  • 29
  • 37
  • Perhaps your mysql table has changed, otherwise it's most likely a typo somwhere in `$sql = "TYPO SOMEWHERE HERE"` – Emmanuel Apr 13 '11 at 08:31

3 Answers3

4

That means the query failed. Usually it's a SQL syntax error. To find out, just insert this right before the _fetch_assoc line:

print mysql_error();

To prevent the error message, structure your code like this to check the $result beforehand:

$sql = "SELECT * FROM $tbl_name WHERE....";

if ($result = mysql_query($sql)) {
    $row = mysql_fetch_assoc($result);         
}
else print mysql_error();
mario
  • 144,265
  • 20
  • 237
  • 291
1

Always run all your queries this way

$sql = "SELECT * FROM $tbl_name WHERE....";
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
$row = mysql_fetch_assoc($result);

And you will be notified of the cause of error.

But never print or let die() output any errors, as it's security flaw.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

This error usually occurs because no data is returned from the query. Make sure the data is being returned by going into something like PHPMyAdmin and making sure the query returns some rows. You should also add the

or die(mysql_error());

At the end of your query.

Tom Walters
  • 15,366
  • 7
  • 57
  • 74
  • 3
    Every time someone perpetuates the `foo() or die()` meme, the coding gods kill a newbie. Please, won't somebody think of the newbies!? (*Killing your script* when an error happens with no chance to clean up after yourself is a bad design and a bad habit to get in to.) – Charles Apr 13 '11 at 08:38
  • @Charles: On the other hand, don't we _want_ the newbies dead? :P – Lightness Races in Orbit Apr 13 '11 at 08:45
  • Sorry guys, I'm just too used to brute force debugging :) – Tom Walters Apr 13 '11 at 08:50