如果在 SQL 中找不到搜索,则显示错误?

Display error if search cannot be found in SQL?

为简单起见,我的搜索框在外部数据库中搜索食物。 搜索工作正常。如果我搜索披萨,代码会在数据库中找到披萨并显示它。如果我搜索 chicken,代码会找到 chicken。在数据库等中

<?php

$searchquery = mysql_real_escape_string(trim($_POST['searchquery'])); //this is how we search and display results 
$find_searchengine=mysql_query("SELECT * FROM searchengine WHERE food_name LIKE '%$searchquery%'"); //we look within the external database 
while($row = mysql_fetch_assoc($find_searchengine))


{
    $food_name = $row['food_name'];

    echo "$food_name<br / >";  //Sorry our servers are down today 

    }

?>

问题是,我的数据库当然不包含世界上所有的食物。所以,如果我搜索让我们说 "Rice",什么也不会出现,页面不会加载任何东西,它不会以任何方式改变,就像按下回车按钮一样。

如果在数据库中找不到某种食物,谁能指导我如何显示错误的正确方向。

如果查询正在获取任何记录,只需添加一个检查。试试 -

if(mysql_num_rows($find_searchengine) > 0) {
     // process data
} else {
     //display error
}

这样试试:

if(mysql_num_rows($find_searchengine) > 0){
while($row = mysql_fetch_assoc($find_searchengine))
{
    $food_name = $row['food_name'];

    echo "$food_name<br / >";  //Sorry our servers are down today 

    }
}else{
      echo "not match found for search criteria";
}

注意:最重要的是停止使用 mysql 并启动 mysqli 或 pdo。

   if(empty($row['food_name']))
    print"Food is not found":