6

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:

<?php

mysql_connect("localhost","username","password");
mysql_select_db("database");

if(!empty($_GET['q'])){
$query=mysql_real_escape_string(trim($_GET['q']));
$searchSQL="SELECT * FROM links WHERE `title` LIKE '%{$query}%'  LIMIT 8";
$searchResult=mysql_query($searchSQL);

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

?>
0

8 Answers 8

6
if (empty($results)) { 
    echo 'No results found'; 
} else {
    echo implode($results);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This will fail either way, because the while loop would have run at this point, and an error would have been issued.
This worked great for me! ..thanks. @Shef, the while loops goes where the echo appears above, it is not evaluated before the if statements. You should at least test it first before making a comment. And if you did test it, and it didn't work, then i am interested in seeing that new situation of yours.
4

Create a MYSQL Connection and paste this code below

$sql="SELECT * FROM tablename WHERE columnname LIKE your variable or constant ";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count>=1){if result was found}

else {if result was not found}

?>

Comments

3

You could count the number of elements in the array, and either continue with your implode or display the message you mentioned.

<?php
     if(count($results) > 0){
         echo implode($results);
     }
     else {
         echo "No results were found.";
     }
?>

You also really should not be using the mysql_* functions. Use either the improved version (mysqli_*) or PDO.

Comments

1

Try the following:

<?php

mysql_connect("localhost","username","password");
mysql_select_db("database");

if(!empty($_GET['q'])){
$query=mysql_real_escape_string(trim($_GET['q']));
$searchSQL="SELECT * FROM links WHERE `title` LIKE '%{$query}%'  LIMIT 8";
$searchResult=mysql_query($searchSQL);

if(mysql_num_rows($searchResult) <= 0)
{
    echo "No results";
} else {

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

?>

Also please either use MySQLi or PDO as it is safer and better to use, some information can be found below. Personally I prefer MySQLi but prepared statements in PDO is really good and saves some lines of code every time you query ;)

MySQLi and PHP

PDO and PHP

1 Comment

This will give an error on the last line. Move echo implode($results) between the brackets.
1
<?php

mysql_connect("localhost","username","password");
mysql_select_db("database");

if(!empty($_GET['q'])){
    $query          =   mysql_real_escape_string(trim($_GET['q']));
    $searchSQL      =   "SELECT * FROM links WHERE `title` LIKE '%{$query}%'  LIMIT 8";
    $searchResult   =   mysql_query($searchSQL);

    // the query was run successfully 
    // and it returned at least a result
    if(is_resource($searchResult) && mysql_num_rows($result) > 0){
        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);
    } else{
        echo 'No Results were found';
    }
}
?>

Comments

0

You could also use the function mysql_num_rows, which will tell you the number of rows returned by your query.

$rows = mysql_num_rows($searchResult);
if($rows <= 0){
    /* Code if there are no rows */
}
else{
    /* At least one row has been found */
}

Comments

0
if (mysql_num_rows($searchResult) == 0) {
   echo "some error message"; 
} else { 
  ... process data
}

Comments

0

How about a simple counter in the while statement:

$i = 0;

while (condition) {
   $i++;
   do stuff;
}

if ($i ==0) {echo 'No results found';}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.