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
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)) {
    }
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.