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();
}
?>

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.

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?

:/

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();
?>
commented: took the time to help me out after I got confused with my else if - great guy!!! :) +1

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.