开发者

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here: Closed 10 years ago.

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($resu开发者_运维技巧lt);

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

Any ideas??


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();


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜