phpQuestioner Posted March 13, 2007 Share Posted March 13, 2007 Does anyone know what would be the best way display content within your page; if the is no rows in your database? I was thinking something like this, but no dice - any one know the cure for this problem? while($r=mysql_fetch_array(!$result)) { header("Location: index.html"); } Link to comment https://forums.phpfreaks.com/topic/42569-what-is-the-best-way-to-display-content-if-there-is-no-rows-in-database/ Share on other sites More sharing options...
papaface Posted March 13, 2007 Share Posted March 13, 2007 $num = mysql_num_rows($sql); if ($num == 0) { echo "No results"; } Link to comment https://forums.phpfreaks.com/topic/42569-what-is-the-best-way-to-display-content-if-there-is-no-rows-in-database/#findComment-206564 Share on other sites More sharing options...
trq Posted March 13, 2007 Share Posted March 13, 2007 <?php // connect. if ($result = mysql_query("SELECT * FROM tbl")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { // display data. } } else { echo "No records found"; } } else { echo "Query failed"; } ?> Link to comment https://forums.phpfreaks.com/topic/42569-what-is-the-best-way-to-display-content-if-there-is-no-rows-in-database/#findComment-206567 Share on other sites More sharing options...
phpQuestioner Posted March 13, 2007 Author Share Posted March 13, 2007 Right now I have something like this: Page.php <table> <tr> <td>Id</td> <td>Type</td> </tr> <?php mysql_connect("localhost", "username", "password"); mysql_db_select("myDatabase"); $results = mysql_query("select * from myRows"); if (while($r=mysql_fetch_array($result) == 0)) { header("Location: Page.php?sort=asc"); } else { while($r=mysql_fetch_array($result)) $id=$r["id"]; $type=$r["type"]; print "<tr><td>$id</td><td>$type</td></tr>"; } // Would I Just Add Another Else Statement Here - I Think I Tried That And The Page Would Not Even Display else { header("Location: index.html"); } ?> </table> Would I just add another else statement (please look at author comment in code)? Link to comment https://forums.phpfreaks.com/topic/42569-what-is-the-best-way-to-display-content-if-there-is-no-rows-in-database/#findComment-206595 Share on other sites More sharing options...
papaface Posted March 13, 2007 Share Posted March 13, 2007 Your code doesnt really make sense, I think this is what you want: This wil be your code: <table> <tr> <td>Id</td> <td>Type</td> </tr> mysql_db_connect("localhost", "username", "password"); mysql_db_select("myDatabase"); $results = mysql_query("select * from myRows"); while($r=mysql_fetch_array($results)) { if (mysql_num_rows($results) == 0) { echo "No results"; } else { $id=$r["id"]; $type=$r["type"]; print "<tr><td>$id</td><td>$type</td></tr>"; } </table> Link to comment https://forums.phpfreaks.com/topic/42569-what-is-the-best-way-to-display-content-if-there-is-no-rows-in-database/#findComment-206601 Share on other sites More sharing options...
trq Posted March 13, 2007 Share Posted March 13, 2007 My code is a good example of how to query a database. papaface's is error prone. Link to comment https://forums.phpfreaks.com/topic/42569-what-is-the-best-way-to-display-content-if-there-is-no-rows-in-database/#findComment-206602 Share on other sites More sharing options...
papaface Posted March 13, 2007 Share Posted March 13, 2007 @ thorpe For my own knowledge can you explain to me how it is error prone? Thanks Link to comment https://forums.phpfreaks.com/topic/42569-what-is-the-best-way-to-display-content-if-there-is-no-rows-in-database/#findComment-206604 Share on other sites More sharing options...
trq Posted March 13, 2007 Share Posted March 13, 2007 You never check to see if your $results variable holds a valid result resource before using it. Link to comment https://forums.phpfreaks.com/topic/42569-what-is-the-best-way-to-display-content-if-there-is-no-rows-in-database/#findComment-206608 Share on other sites More sharing options...
papaface Posted March 13, 2007 Share Posted March 13, 2007 Thanks Link to comment https://forums.phpfreaks.com/topic/42569-what-is-the-best-way-to-display-content-if-there-is-no-rows-in-database/#findComment-206612 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.