在PHP MySQL搜索中找不到结果时显示消息 [英] Displaying message when no results found in PHP MySQL search
本文介绍了在PHP MySQL搜索中找不到结果时显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询MySQL数据库的PHP搜索脚本。目前,当没有显示结果时,脚本显示并显示错误。如何在没有任何内容返回时显示没有找到结果的消息?
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?
我的PHP脚本是:
<?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);
}
?>
推荐答案
if (empty($results)) {
echo 'No results found';
} else {
echo implode($results);
}
这篇关于在PHP MySQL搜索中找不到结果时显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文