izbryte Posted December 8, 2007 Share Posted December 8, 2007 I have this query and I'm trying to echo some text if there are no results but it's not working. I'm sure this is either me being a bonehead or it's been WAY too long since I took my last mySQL class... LOL Here's what I have: $query = "SELECT * from firms ORDER BY firmID DESC"; $result = mysql_query($query); if ($result){ while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo (whatever the results were); } } else{ echo "No results found."; } My results are coming up fine but if it's empty I just see blank.... Thanks! Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted December 8, 2007 Share Posted December 8, 2007 $result will return true whether or not any rows was returned or not. What you should use is mysql_num_rows to see if any rows was returned or not, ge: $query = "SELECT * from firms ORDER BY firmID DESC"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { echo "No results found."; } else { while($row = mysql_fetch_assoc($result)) { echo (whatever the results were); } } Quote Link to comment Share on other sites More sharing options...
marcus Posted December 8, 2007 Share Posted December 8, 2007 <?php $query = "SELECT * FROM `firms` ORDER BY firmid DESC"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ $text = (strlen($row['field']) > 0) ? "{$row['field']}<br>" : ""; echo $text; } }else { echo "There are no firms available"; } ?> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 8, 2007 Share Posted December 8, 2007 <?php $query = "SELECT * from firms ORDER BY firmID DESC"; $result = mysql_query($query); $noresults = mysql_num_rows($result); if ($noresults != 0){ while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo (whatever the results were); } } else{ echo "No results found."; } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 $query = "SELECT * from firms ORDER BY firmID DESC"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result) while($row ) { echo (whatever the results were); } } else { echo "No results found."; } Quote Link to comment Share on other sites More sharing options...
izbryte Posted December 8, 2007 Author Share Posted December 8, 2007 Wow you guys are fast! AND SO SMART! Thanks! Quote Link to comment Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 So many ways to skin a cat 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.