0

I am trying to echo the message 'No records found' when I get nothing back from the database.

I have done a row count which shows me how many rows I have but I can't seem to echo the 'No records found' message. Is it in the wrong place? Will it not run for some reason?

    <?php if(isset($search_results)){
   foreach($search_results as $result) {
       $rowcount = $result['rowcount'];
  if(!$rowcount < 1) {
      echo $rowcount;
         echo '<div class="search_result"> <b>'.$result['title'].'</b><br />';
   echo '<span class="search_result_url">'.$result['link'].'</span><br />';
  echo $result['text'].'<br /></div>';

  } else {
    echo  'No records found.';
  } 
    }
    } else {
        echo 'Use the search to find what you are looking for. Enter the term or keyord into the search box and press enter..';
    }

    ?>
hairynuggets
  • 3,191
  • 22
  • 55
  • 90
  • Just check for `if ($rowcount > 0) {` – Leonid Shevtsov Oct 31 '11 at 13:24
  • The code suggests that you may be unclear as to whether $search_results is an array of rows (in which case $search_results itself is your "result set") or if it is an array of arrays, each of which has a 'rowcount' field (and who knows what else). It would be helpful if you could describe exactly the format of the data which you expect to find inside the $search_results array. – Peter Oct 31 '11 at 13:29

1 Answers1

7

Take a look at mysql_num_rows(), and use an if() to see whether your mysql_query() returned any results:

$result = mysql_query("SELECT * FROM table");

if(!mysql_num_rows($result))
{
    echo 'No results';
}
else
{
    // Results - do stuff.
}
Bojangles
  • 99,427
  • 50
  • 170
  • 208