PcGeniusProductions Posted January 3, 2009 Share Posted January 3, 2009 Hi guys. I am trying to create an addon for my chat script, and so far so good. I am stuck with a rather embarresingly simple PHP Statement. The script below works fine, but I want it to display "Empty" if there are no SQL Results. E.g. If it returns values, then the Column is not empty, if it does not, the column IS empty, so display [echo "Empty";] I cannot seem to do this. Here is my code. I will be grateful for any tips you can give me. <?php $sql = "SELECT column FROM table WHERE is_online = 1"; $results = mysql_query($sql); $newline = "<br />"; while($row = mysql_fetch_array($results)) { echo "" . $row['column'] . "" . $newline . ""; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139316-simple-ifelse-statements-please-help/ Share on other sites More sharing options...
GingerRobot Posted January 3, 2009 Share Posted January 3, 2009 Try using mysql_num_rows: <?php $sql = "SELECT column FROM table WHERE is_online = 1"; $results = mysql_query($sql); if(mysql_num_rows($results) > 0){ $newline = "<br />"; while($row = mysql_fetch_array($results)) { echo "" . $row['column'] . "" . $newline . ""; } }else{ echo 'Empty'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139316-simple-ifelse-statements-please-help/#findComment-728673 Share on other sites More sharing options...
PcGeniusProductions Posted January 3, 2009 Author Share Posted January 3, 2009 Aha! This is what I was doing wrong. I had the "else" part setup OK. I just never told it what the else was reffering from. (if that makes sense ) GingerRobot, this is perfect. thank you very much! Regards, Reece. Quote Link to comment https://forums.phpfreaks.com/topic/139316-simple-ifelse-statements-please-help/#findComment-728674 Share on other sites More sharing options...
zeeli Posted January 3, 2009 Share Posted January 3, 2009 Try this: <?php $sql = "SELECT column FROM table WHERE is_online = 1"; $results = mysql_query($sql); $num_rows = mysql_num_rows($results); $newline = "<br />"; if($num_rows > 0) { while($row = mysql_fetch_array($results)) { echo "" . $row['column'] . "" . $newline . ""; } } else { echo "Empty"; } ?> EDIT: GingerRobot beat me Regards, zEeLi Quote Link to comment https://forums.phpfreaks.com/topic/139316-simple-ifelse-statements-please-help/#findComment-728675 Share on other sites More sharing options...
PcGeniusProductions Posted January 3, 2009 Author Share Posted January 3, 2009 Hey Zeeli. Regardless of who got there first, I am still very grateful for your willingness to help out. I tried the scrips and they both work as I needed. Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/139316-simple-ifelse-statements-please-help/#findComment-728678 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.