在 PHP MySQL 搜索中未找到结果时显示消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6479977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:30:36  来源:igfitidea点击:

Displaying message when no results found in PHP MySQL search

phpmysqlhtmlsql

提问by Callum Whyte

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?

我有一个查询 MySQL 数据库的 PHP 搜索脚本。目前,当没有显示任何结果时,脚本会显示并出错。当没有返回任何内容时,如何使其显示类似“未找到结果”的消息?

My PHP script is:

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

?>

采纳答案by Sjoerd

if (empty($results)) { 
    echo 'No results found'; 
} else {
    echo implode($results);
}

回答by Heart

Create a MYSQL Connection and paste this code below

创建一个 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}

?>

回答by Tieson T.

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.

您也不应该使用 mysql_* 函数。使用改进版本 (mysqli_*) 或 PDO。

回答by Shef

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

回答by Marc Towler

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 或 PDO,因为它更安全,更好用,一些信息可以在下面找到。我个人更喜欢 MySQLi,但 PDO 中的预处理语句非常好,每次查询时都可以节省一些代码行;)

MySQLi and PHP

MySQLi 和 PHP

PDO and PHP

PDO 和 PHP

回答by Chris9

How about a simple counter in the while statement:

while 语句中的简单计数器怎么样:

$i = 0;

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

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

回答by drfranks3

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

您还可以使用函数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 */
}

回答by Scott C Wilson

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