Jump to content

What Is The Best Way To Display Content If There Is No Rows In Database?


phpQuestioner

Recommended Posts

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");
}

<?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";
 }

?>

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)?

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.