Ameslee Posted February 12, 2007 Share Posted February 12, 2007 hope somene can help, just wondering how i would display a msg (not a pop up) on the page if there is nothing to be displayed from the database - something like: Check back soon any help appreciated, thanks Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/ Share on other sites More sharing options...
trq Posted February 12, 2007 Share Posted February 12, 2007 <?php // connect to db $sql = "SELECT data FROM foo"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // display data } else { //no records found. echo "come back soon"; } } else { echo "query failed ".mysql_error(); } ?> Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/#findComment-182325 Share on other sites More sharing options...
Ameslee Posted February 12, 2007 Author Share Posted February 12, 2007 thanks, could that be put into my existing code? heres my exisiting code with the new code added in: <?php $hostname = localhost; $username = ; $password = ; $conn = mysql_connect($hostname, $username, $password) or die(mysql_error()); $connection = mysql_select_db("greenac_VORNExpo", $conn); $query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC"; $mysql_result=mysql_query($query,$conn); $row=mysql_fetch_array($mysql_result); echo "<table width=70% border=0 cellpadding=0 cellspacing=0>"; $i=1; while (($row!="") && ($i<=7)) { $date=$row[2]; $day=substr($date,8,2); $month=substr($date,5,2); $year=substr($date,0,4); $date2=$day." - ".$month." - ".$year; $event = $row[1]; echo("<tr><td width=30%>$event</td><td width=15%>$date2</td></tr> "); $row=mysql_fetch_array($mysql_result); $i++; } else { //no records found. echo "come back soon"; } } else { echo "query failed ".mysql_error(); } ?> Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/#findComment-182328 Share on other sites More sharing options...
Ameslee Posted February 12, 2007 Author Share Posted February 12, 2007 ok that cant be right coz i got an error Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/#findComment-182334 Share on other sites More sharing options...
steelmanronald06 Posted February 12, 2007 Share Posted February 12, 2007 Try this. Copy and paste should work. <?php $hostname = localhost; $username = ; $password = ; $conn = mysql_connect($hostname, $username, $password) or die(mysql_error()); $connection = mysql_select_db("greenac_VORNExpo", $conn); $query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC"; $mysql_result=mysql_query($query,$conn); $login_check = mysql_num_rows($query); if($login_check > 0){ echo "<table width=70% border=0 cellpadding=0 cellspacing=0>"; $i=1; while (($row = mysql_fetch_array($mysql_result)) && ($i<=7)) { $date=$row[2]; $day=substr($date,8,2); $month=substr($date,5,2); $year=substr($date,0,4); $date2=$day." - ".$month." - ".$year; $event = $row[1]; echo("<tr><td width=30%>$event</td><td width=15%>$date2</td></tr>"); $i++; } } else { echo "No data!"; } else { echo "query failed ".mysql_error(); } ?> Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/#findComment-182341 Share on other sites More sharing options...
Ameslee Posted February 12, 2007 Author Share Posted February 12, 2007 Parse error: syntax error, unexpected T_ELSE in /home/greenac/public_html/events.php on line 222 i get this error! Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/#findComment-182353 Share on other sites More sharing options...
Ameslee Posted February 12, 2007 Author Share Posted February 12, 2007 any ideas, why this error is showing? Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/#findComment-182359 Share on other sites More sharing options...
Ameslee Posted February 12, 2007 Author Share Posted February 12, 2007 i have changed the code and im getting a different error. here is the code: <?php $hostname = localhost; $username = greenac_Admin; $password = greenac; $conn = mysql_connect($hostname, $username, $password) or die(mysql_error()); $connection = mysql_select_db("greenac_VORNExpo", $conn); $query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC"; $mysql_result=mysql_query($query,$conn); echo "<table width=70% border=0 cellpadding=0 cellspacing=0>"; $nothingInDatabase= true; $i = 1 while ($row = mysql_fetch_array($mysql_result) && $i<=7) { if ($nothingInDatabase) $nothingInDatabase = false; $date=$row[2]; $day=substr($date,8,2); $month=substr($date,5,2); $year=substr($date,0,4); $date2=$day." - ".$month." - ".$year; $event = $row[1]; echo("<tr><td width=30%>$event</td><td width=15%>$date2</td></tr>"); $i++; } if ($nothingInDatabase == true) { echo "<tr><td>Please check back later for entries</td></tr>"; } echo "</table>"; ?> the new error is: Parse error: syntax error, unexpected T_WHILE in /home/greenac/public_html/events.php on line 201 Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/#findComment-182423 Share on other sites More sharing options...
fert Posted February 12, 2007 Share Posted February 12, 2007 $i = 1 there's your problem Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/#findComment-182425 Share on other sites More sharing options...
Ameslee Posted February 12, 2007 Author Share Posted February 12, 2007 yeah thanks, i found that. Link to comment https://forums.phpfreaks.com/topic/38088-displaying-a-msg-if-nothing-in-database/#findComment-182432 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.