Display a message when no results are found in PHP

I have a search for a PHP script that queries a MySQL database. Currently, when the results are not displayed, the script and error are displayed. How can I make it display a β€œNo Results Found” message when nothing is returned?

My PHP script:

<?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); } ?> 
+4
source share
8 answers
 if (empty($results)) { echo 'No results found'; } else { echo implode($results); } 
+3
source

You can count the number of elements in the array and either continue your untying, or display the message you mentioned.

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

You should also not use mysql_ * functions. Use either the enhanced version (mysqli_ *) or PDO.

+3
source

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} ?> 
+3
source

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 use MySQLi or PDO, as it is safer and better to use, some information can be found below. Personally, I prefer MySQLi, but the prepared statements in PDO are really good and save some lines of code every time you query;)

MySQLi and PHP

PDO and PHP

+1
source
 <?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'; } } ?> 
+1
source

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

 $rows = mysql_num_rows($searchResult); if($rows <= 0){ /* Code if there are no rows */ } else{ /* At least one row has been found */ } 
0
source
 if (mysql_num_rows($searchResult) == 0) { echo "some error message"; } else { ... process data } 
0
source

How about a simple counter in a while statement:

 $i = 0; while (condition) { $i++; do stuff; } if ($i ==0) {echo 'No results found';} 
0
source

All Articles