我有一个查询MySQL数据库的PHP搜索脚本。当前,当未显示任何结果时,脚本显示并显示错误。当什么都没有返回时,如何使它显示诸如“未找到结果”之类的消息?
我的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); }