如何显示“找不到结果”消息(PHP)

时间:2013-12-08 07:30:35

标签: 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
?>

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

4 个答案:

答案 0 :(得分:1)

设置一个标志计数器,你就可以了。

$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

答案 1 :(得分:0)

在while()循环之前将变量设置为0。在循环内将其设置为1。如果循环后它仍为0,则打印一些文本。像这样:

$found=0;
while($row = mysql_fetch_array($result)){
    $found=1;
    ...
}//end while loop
if ($found==0) {
    echo "no results found";
}

答案 2 :(得分:0)

您需要查看是否返回了任何行。根据{{​​3}}:

  

使用mysql_num_rows()来查找为a返回的行数   SELECT语句或mysql_affected_rows()来查找多少行   受到DELETE,INSERT,REPLACE或UPDATE语句的影响。

这样的事情会对您的代码有所帮助:

...
$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%' ");

 if (mysql_num_rows($result) == 0 ) {
    // display no results found
 } else {
    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

    }
  }
...


}

答案 3 :(得分:0)

易。只需采用那些echo语句&amp;将它们放在变量中。如果变量不为空,则为echo。如果该声明为空,echo您的“未找到结果。”消息。调整后的代码如下:

if(isset($_POST['submit'])){
    $ret = '';
    $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'];

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

    }

    // Check if '$ret' has content or not.
    if (!empty($ret)) {
        echo $ret;
    }
    else {
        echo "<h2><em>No results were found.</em></h2>";
    }

  }//end if submit