Cosmo Posmo Cosmo Posmo - 6 months ago 45
PHP Question

Displaying message when no results found in PHP MySQL search

I have a PHP search script which queries a MySQL database. Currently, when no results are displayed the script shows and error. How can I make it display a message like "No Results were found" when nothing is returned?

My PHP script is:



$searchSQL="SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 8";

while ($row=mysql_fetch_assoc($searchResult)){
$results[]="<div class='webresult'><div class='title'><a href='{$row['url']}'>{$row['title']}</a></div><div class='desc'>{$row['description']}</div><div class='url'>{$row['url']}</div></div>";

echo implode($results);


if (empty($results)) { 
    echo 'No results found'; 
} else {
    echo implode($results);