Jump to content

[SOLVED] Quick question


onthespot

Recommended Posts

I am just starting to get into the swing of things, making a lot of progress in the early stage of PHP. However, I have come across a problem.

 

<table>

<?

 

$res=mysql_query("SELECT * FROM ".TBL_NEWS." ORDER BY date DESC");

 

while($row=mysql_fetch_assoc($res)){

 

$posted=$row['posted'];

$date=$row['date'];

$comment=$row['comment'];

$subject=$row['subject'];

?>

 

<tr><td><?echo "$subject\n";?></td></tr>

<tr><td><?echo "at $date\n";?></td></tr>

<tr><td><?echo "$comment\n";?></td></tr>

<tr><td><?echo "Posted by $posted\n";?></td></tr>

<tr><td><?echo "<br />\n";?></td></tr>

 

<?

}

?>

</table>

 

And here is the error

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

 

I have spent 20 minutes, and just can't see the error. Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/165212-solved-quick-question/
Share on other sites

How can I check these?

 

They usually fill up your screen ;)

 

Anyway, try:

 

$res=mysql_query(.., $db /* if working with multiple db's don't forget to add the db resource */);
print mysql_num_rows($res); // debug
if (mysql_num_rows($res)) { // now only executes if their actually is something
    while ($row = mysql_fetch_assoc($res)) {
    }
}

Sorry to pick.

 

mysql_fetch_assoc() only produces that error when the query fails and returns a FALSE value. A query that succeeds but contains zero rows does not generate an error because it does return a valid result resource, with zero rows in it. The code just posted with mysql_num_rows() will produce the same error, but on the mysql_num_rows() statement instead.

 

Screens full of php notices and warnings mean that your php code is experiencing problems that need to be addressed. Php code should not generate any errors, warnings, or notices during its' normal execution. Each line of php code that contains such an error, even if the error_reporting/display_errors settings or using an @ prevents the output of the actual message, takes at least 10x longer to execute because php must still go through the error response handler to try and figure out what to do about the error.

 

To find out why your query is failing, use the following for debugging purposes -

 

$res=mysql_query("SELECT * FROM ".TBL_NEWS." ORDER BY date DESC") or die(mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.