我有一个查询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);
}
?>
答案 0 :(得分:4)
if (empty($results)) {
echo 'No results found';
} else {
echo implode($results);
}
答案 1 :(得分:4)
创建MYSQL连接并将此代码粘贴到
下面$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}
?>
答案 2 :(得分:3)
您可以计算数组中元素的数量,并继续进行内爆或显示您提到的消息。
<?php
if(count($results) > 0){
echo implode($results);
}
else {
echo "No results were found.";
}
?>
你也真的不应该使用mysql_ *函数。使用改进版本(mysqli_ *)或PDO。
答案 3 :(得分: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);
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);
}
}
?>
另外请使用MySQLi或PDO,因为它更安全,更好用,有些信息可以在下面找到。我个人更喜欢MySQLi,但PDO中的预处理语句非常好,每次查询时都会保存一些代码行;)
答案 4 :(得分: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';
}
}
?>
答案 5 :(得分:0)
您还可以使用函数mysql_num_rows
,它将告诉您查询返回的行数。
$rows = mysql_num_rows($searchResult);
if($rows <= 0){
/* Code if there are no rows */
}
else{
/* At least one row has been found */
}
答案 6 :(得分:0)
if (mysql_num_rows($searchResult) == 0) {
echo "some error message";
} else {
... process data
}
答案 7 :(得分:0)
while语句中的简单计数器怎么样:
$i = 0;
while (condition) {
$i++;
do stuff;
}
if ($i ==0) {echo 'No results found';}