onthespot Posted July 8, 2009 Share Posted July 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/ Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 I'd recommend checking if it returned a mysql error. Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871188 Share on other sites More sharing options...
onthespot Posted July 8, 2009 Author Share Posted July 8, 2009 It didnt actually, thats the thing! Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871190 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 How about php errors? Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871193 Share on other sites More sharing options...
onthespot Posted July 8, 2009 Author Share Posted July 8, 2009 How can I check these? Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871194 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 Add this to the script: <?php /* Enable displaying of errors */ error_reporting(E_ALL); ini_set('display_errors', 'On'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871196 Share on other sites More sharing options...
ignace Posted July 8, 2009 Share Posted July 8, 2009 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)) { } } Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871198 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 lol - if your code is clean you shouldn't have any Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871200 Share on other sites More sharing options...
onthespot Posted July 8, 2009 Author Share Posted July 8, 2009 That error code is awesome. From that I realised I had made a silly error of not including the files I needed at the top. ouch! Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871207 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 Glad it helped you out... could you mark the topic as solved? Cheers, p2grace Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871208 Share on other sites More sharing options...
PFMaBiSmAd Posted July 8, 2009 Share Posted July 8, 2009 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()); Quote Link to comment https://forums.phpfreaks.com/topic/165212-solved-quick-question/#findComment-871211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.