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! Link to comment https://forums.phpfreaks.com/topic/80813-solved-an-easy-one-i-think/ 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); } } Link to comment https://forums.phpfreaks.com/topic/80813-solved-an-easy-one-i-think/#findComment-409950 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/80813-solved-an-easy-one-i-think/#findComment-409951 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."; } ?> Link to comment https://forums.phpfreaks.com/topic/80813-solved-an-easy-one-i-think/#findComment-409952 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."; } Link to comment https://forums.phpfreaks.com/topic/80813-solved-an-easy-one-i-think/#findComment-409954 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! Link to comment https://forums.phpfreaks.com/topic/80813-solved-an-easy-one-i-think/#findComment-409960 Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 So many ways to skin a cat Link to comment https://forums.phpfreaks.com/topic/80813-solved-an-easy-one-i-think/#findComment-409963 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.