eRott Posted December 10, 2007 Share Posted December 10, 2007 I have a section of code which grabs the content from the database: $sql = "SELECT * FROM beepbopboop WHERE entry_id = '$entryid' ORDER BY id DESC"; $result = mysql_query($sql, $conn) or die(mysql_error()); while ($list = mysql_fetch_array($result)) { echo "blah blah blah {$list['how_to_build_an_icbm']} blah blah blah"; } How would I check to see if any content (rows) have been returned, and if there are none, display something else? Thanks, Regards. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 <?php $sql = "SELECT * FROM beepbopboop WHERE entry_id = '$entryid' ORDER BY id DESC"; $result = mysql_query($sql, $conn) or die(mysql_error()); if (mysql_num_rows($result) < 1){ //no results returned } else { //results found } ?> Quote Link to comment Share on other sites More sharing options...
eRott Posted December 10, 2007 Author Share Posted December 10, 2007 That's basically what I tried (slightly different code), however, it needs to be in a while statement. With this code, nothing happens. No error's. Just, nothing. $sql = "SELECT * FROM blahblah WHERE entry_id = '$entryid' ORDER BY id DESC"; $result = mysql_query($sql, $conn) or die(mysql_error()); $chknum = mysql_num_rows($result); while ($list = mysql_fetch_array($result)) { if ($chknum < 1) { echo "blah blah NO RESULTS"; } else { echo "blah blah RESULTS FOUND"; } } Any ideas? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 Well, if there are no results, then your while loop isn't going to execute any code in between. So I'm assuming no results are being returned. Quote Link to comment Share on other sites More sharing options...
eRott Posted December 10, 2007 Author Share Posted December 10, 2007 I noticed and fixed it within the time it took you to post that reply. Anyway: $sql = "SELECT * FROM APPLE_ORANGE WHERE entry_id = '$entryid' ORDER BY id DESC"; $result = mysql_query($sql, $conn) or die(mysql_error()); $chknum = mysql_num_rows($result); if ($chknum < 1) { // NO RESULTS } else { while ($list = mysql_fetch_array($result)) { // RESULTS } } Thanks for your help pocobueno1388. Take care. Quote Link to comment 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.