0

hi everyone,

I've written some code that displays information from a database. However, I added more code that would display a "no records found" message, should the area be blank.

The code displays the records perfectly, however if there are no records returned it doesn't show the "no records found" message.

Can someone please have a quick look and tell me where I'm going wrong? I know it's going to be something simple, but that just makes it even worse!!!

Thanks guys :)

<?php
mysql_connect(HOST,USER,PASS);
@mysql_select_db("db303278079") or die( "Unable to select database");
$query="select * from ".RECORDS." WHERE area='manchester' order by link_text ASC";
$result=mysql_query($query);
$num=mysql_num_rows($result);
for ($i=0; $i<$num; $i++) {
$rest_name=mysql_result($result,$i,"link_text");
$address=mysql_result($result,$i,"address");
$telephone=mysql_result($result,$i,"telephone");
$website=mysql_result($result,$i,"url");
$cuisine_type=mysql_result($result,$i,"cuisine_type");
$area=mysql_result($result,$i,"area");
$website=(empty($website))?'':", <strong><A HREF=$website>Website</a></strong>";
$count = @mysql_num_rows($result);
if($count > 0)
{
     echo "<strong>$rest_name</strong>, $address, $telephone, ($cuisine_type)$website<br><br >";
}
else
{
    echo "no records found";
}  
mysql_close();
}
?>
2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by dmkc
0

The if ($count) is inside the for loop, which doesn't get executed if there are no records found. Place the if ($count == 0) outside the for loop to fix it.

0

hmm, thanks for the reply, I've tried moving it before the for loop (if I do this is displays the "no records found" message even though there are 20 plus records) and if i move it after it just displays the one record..... I know I'm being ultra blonde here but what am I not doing right?

:/

1

Try this:

<?php
  mysql_connect(HOST,USER,PASS);
  @mysql_select_db("db303278079") or die( "Unable to select database");
  
  $query="select * from ".RECORDS." WHERE area='manchester' order by link_text ASC";
  $result=mysql_query($query);
  
  $num=mysql_num_rows($result);
  if ($num == 0)
  {
    echo "no records found";
  }
  else
  {
    for ($i=0; $i<$num; $i++) {
      $rest_name=mysql_result($result,$i,"link_text");
      $address=mysql_result($result,$i,"address");
      $telephone=mysql_result($result,$i,"telephone");
      $website=mysql_result($result,$i,"url");
      $cuisine_type=mysql_result($result,$i,"cuisine_type");
      $area=mysql_result($result,$i,"area");
      $website=(empty($website))?'':", <strong><A HREF=$website>Website</a></strong>";
      $count = @mysql_num_rows($result);
      echo "<strong>$rest_name</strong>, $address, $telephone, ($cuisine_type)$website<br><br >";
    }  
  }
  mysql_close();
?>
Votes + Comments
took the time to help me out after I got confused with my else if - great guy!!! :)
0

that works absolutely perfectly, thankyou for your help matey, much appreciated!!!!! :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.