php - 如何显示 "No Results Found"消息 (PHP)

标签 php mysql database search

只是想知道当我在搜索栏中输入内容但没有任何内容与 MySQL 数据库中存储的内容匹配时,如何显示此消息。

到目前为止我所拥有的是这个。

<?php

if(isset($_POST['submit'])){
    $search = trim($_POST['search']);
    if($search != ""){
        //echo "search: ". $search;
        $result = mysql_query("SELECT * FROM catalogue WHERE 
            name LIKE '$name' OR 
            category LIKE '$category' OR
            brand LIKE '$brand' OR
            season LIKE '$season' OR
            price LIKE '$price' OR 
            store LIKE '$store' OR 
            description LIKE '%$search%' ");

        while($row = mysql_fetch_array($result)){
            $name = $row['name'];
            $file = $row['file'];
            $description = $row['description'];
            $category = $row['category'];
            $brand = $row['brand'];
            $season = $row['season'];
            $price = $row['price'];
            $store = $row['store'];
            $cid = $row['cid'];

            echo "\n<div class=\"thumb\">";
            echo "\n\t<a href=\"single.php?cid=$cid\"><img src=\"thumbs/$file\" class=\"thumbnailImg\" width=\"150\" height=\"200\"/></a><br/>";
            echo "\n\t".$name. " ";
            echo "\n\t$". $price;
            echo "\n</div>";
        }//end while loop

    }else{
        echo "<h2><em>No results were found.</em></h2>";
    }//end if search ,else

  }//end if submit
?>

如果我只是单击搜索而不输入任何内容,则此代码片段有效,但如果我在搜索中输入不匹配的内容,则不会显示任何内容。我该如何解决这个问题?

最佳答案

设置一个标志计数器,你就会让它工作。

$results=0; // Setting a flag here
while($row = mysql_fetch_array($result)){
        $name = $row['name'];
        $file = $row['file'];
        $description = $row['description'];
        $category = $row['category'];
        $brand = $row['brand'];
        $season = $row['season'];
        $price = $row['price'];
        $store = $row['store'];
        $cid = $row['cid'];

        echo "\n<div class=\"thumb\">";
        echo "\n\t<a href=\"single.php?cid=$cid\"><img src=\"thumbs/$file\" class=\"thumbnailImg\" width=\"150\" height=\"200\"/></a><br/>";
        echo "\n\t".$name. " ";
        echo "\n\t$". $price;
        echo "\n</div>";
        $results++; //Incrementing flag if results found.
        }//end while loop

    }
    else if($results==0) 
    {
    echo "<h2><em>No results were found.</em></h2>";
    }
    else{
        echo "<h2><em>No results were found.</em></h2>";
    }//end if search ,else

关于php - 如何显示 "No Results Found"消息 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450958/

相关文章:

mysql - 同一张表上的多个 JOIN

MySQL - 如何诊断警告 "Aborted connection - (Got timeout reading communication packets)"的原因

Java DB (Derby) 数据库更改列大小

javascript - datePicker 仅在双击时显示

php - 如何通过他们的 API 获取 UPS Delivery Progress?

php - 更新表 mysql 的最后 360 行

mysql - 将 Heroku Lumen 应用程序连接到 Amazon RDS MySQL 5.7.19

javascript - 检查输入类型并跳过输入 'password' 字段,同时单击“保存”

database - 绘制 ER 图的令人困惑的场景

sql - MS Access - 自动填充后编辑字段只会编辑第一条记录(表单)