HoTDaWg Posted April 16, 2007 Share Posted April 16, 2007 how do i echo a statement like "There are no results to display." with a code like this?: <?php //connection stuff here.... echo'<table width="375" height="0" cellpadding="0" cellspacing="0" border="1"><tr><td>Id:</td><td>Action:</td><td>Date:</td><td>Ip:</td></tr><tr>'; $sql = "SELECT id,action,date,read,ip FROM actions WHERE read='nope'"; $result = mysql_query($sql,$connection)or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ echo'<td>'.$row['id'].'</td><td>'.$row['action'].'</td><td>'.$row['date'].'</td><td>'.$row['ip'].'</td></tr><tr>'; } echo'</table>'; ?> thanks for any help possible HoTDaWg Link to comment https://forums.phpfreaks.com/topic/47170-solved-echoing-if-there-are-no-results-from-the-db-to-display/ Share on other sites More sharing options...
HeyRay2 Posted April 16, 2007 Share Posted April 16, 2007 You can add a variable that checks how many results were returned from the DB query. If there were no results, then echo a "No results" message. Otherwise, run the while() loop. Something like this: <?php //connection stuff here.... echo'<table width="375" height="0" cellpadding="0" cellspacing="0" border="1"><tr><td>Id:</td><td>Action:</td><td>Date:</td><td>Ip:</td></tr><tr>'; $sql = "SELECT id,action,date,read,ip FROM actions WHERE read='nope'"; $result = mysql_query($sql,$connection)or die(mysql_error()); $num_results = mysql_num_rows($result); if($num_results == 0){ echo "No results to display!"; } else{ while($row = mysql_fetch_assoc($result)){ echo'<td>'.$row['id'].'</td><td>'.$row['action'].'</td><td>'.$row['date'].'</td><td>'.$row['ip'].'</td></tr><tr>'; } ] echo'</table>'; ?> Link to comment https://forums.phpfreaks.com/topic/47170-solved-echoing-if-there-are-no-results-from-the-db-to-display/#findComment-230012 Share on other sites More sharing options...
HoTDaWg Posted April 16, 2007 Author Share Posted April 16, 2007 weird, i am getting the following error. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read,ip FROM actions WHERE read='nope'' at line 1 heres the updated script: <?php //connection stuff echo'<table width="375" height="0" cellpadding="0" cellspacing="0" border="1"><tr><td>Id:</td><td>Action:</td><td>Date:</td><td>Ip:</td></tr><tr>'; $sql = "SELECT id,action,date,read,ip FROM actions WHERE read='nope'"; $result = mysql_query($sql,$connection)or die(mysql_error()); $num_results = mysql_num_rows($result); if($num_results == 0){ echo "No results to display!"; }else{ while($row = mysql_fetch_assoc($result)){ echo'<td>'.$row['id'].'</td><td>'.$row['action'].'</td><td>'.$row['date'].'</td><td>'.$row['ip'].'</td></tr><tr>'; } } echo'</table>'; ?> and my database: -- -- Table structure for table `actions` -- CREATE TABLE `actions` ( `id` int(11) NOT NULL auto_increment, `action` varchar(255) default NULL, `date` varchar(255) default NULL, `read` varchar(255) NOT NULL default '', `ip` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; -- -- Dumping data for table `actions` -- ??? whats goin on... any help would be greatly appreciated thanks HoTDaWg Link to comment https://forums.phpfreaks.com/topic/47170-solved-echoing-if-there-are-no-results-from-the-db-to-display/#findComment-230018 Share on other sites More sharing options...
rcorlew Posted April 16, 2007 Share Posted April 16, 2007 You should use if($result == null){ echo "No results to display!"; }else{ // other query here I have found that using a num_rows on top of a while loop throws the num_rows off for some reason that I am unfamiliar with Link to comment https://forums.phpfreaks.com/topic/47170-solved-echoing-if-there-are-no-results-from-the-db-to-display/#findComment-230085 Share on other sites More sharing options...
HoTDaWg Posted April 16, 2007 Author Share Posted April 16, 2007 thanks for your response:) ouch... You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read,ip FROM actions WHERE read='nope'' at line 1 thanks for your help all i have to go to sleep now but i will bring this topic back up tomorrow if u can, post a reply today and i will check it out asap nightey night and thanks again HoTDaWg Link to comment https://forums.phpfreaks.com/topic/47170-solved-echoing-if-there-are-no-results-from-the-db-to-display/#findComment-230088 Share on other sites More sharing options...
kenrbnsn Posted April 16, 2007 Share Posted April 16, 2007 The word "date" is a reserved word in MySQL. Either change the name of that field or surround the field name with backticks: <?php $sql = "SELECT id,action,`date`,read,ip FROM actions WHERE read='nope'"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/47170-solved-echoing-if-there-are-no-results-from-the-db-to-display/#findComment-230092 Share on other sites More sharing options...
HoTDaWg Posted April 16, 2007 Author Share Posted April 16, 2007 geez this is killing me, check it out i think its the same error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read,ip FROM actions WHERE read='nope'' at line 1 *sigh* KILLING ME:@ herere is the script again: <?php $connection = mysql_connect('localhost','xxxx','XXXXXX'); mysql_select_db('xXXXXXXXX'); echo'<table width="375" height="0" cellpadding="0" cellspacing="0" border="1"><tr><td>Id:</td><td>Action:</td><td>Date:</td><td>Ip:</td></tr><tr>'; $sql = "SELECT id,action,`date`,read,ip FROM actions WHERE read='nope'"; $result = mysql_query($sql,$connection)or die(mysql_error()); $num_results = mysql_num_rows($result); if($num_results == null){ echo "No results to display!"; }else{ while($row = mysql_fetch_assoc($result)){ echo'<td>'.$row['id'].'</td><td>'.$row['action'].'</td><td>'.$row['date'].'</td><td>'.$row['ip'].'</td></tr><tr>'; } } echo'</table>'; ?> thanks again HoTDaWg Link to comment https://forums.phpfreaks.com/topic/47170-solved-echoing-if-there-are-no-results-from-the-db-to-display/#findComment-230623 Share on other sites More sharing options...
per1os Posted April 16, 2007 Share Posted April 16, 2007 Try this bud $sql = "SELECT `id`,`action`,`date`,`read`,`ip` FROM `actions` WHERE `read`='nope'"; Link to comment https://forums.phpfreaks.com/topic/47170-solved-echoing-if-there-are-no-results-from-the-db-to-display/#findComment-230645 Share on other sites More sharing options...
HoTDaWg Posted April 16, 2007 Author Share Posted April 16, 2007 thank you! Link to comment https://forums.phpfreaks.com/topic/47170-solved-echoing-if-there-are-no-results-from-the-db-to-display/#findComment-230722 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.