在没有结果

时间:2017-06-05 02:43:05

标签: php search

我在我的网站上设置了一个搜索按钮。如果有结果,它可以正常工作,但如果没有结果,它就不会显示"没有结果......"。

我的代码出了什么问题?

<html !DOCTYPE HTML>
    <body>
<?php
    $title = "DenTEETH";
    include('header.html');
    //connect to database
    $db = mysqli_connect("127.0.0.1", "root", "", "authentication");


    if(isset($_GET['q']) && $_GET['q'] !== '')
    {
        $searchq = $_GET['q'];
        $sql = "SELECT * FROM search WHERE keyword LIKE '%$searchq%' OR title LIKE '%$searchq%'";
        $output='';
        $results = mysqli_query($db, $sql);

        if (count($results) == 0){
            $output .= 'No search results for <b>"' . $searchq . '"</b>';
        }
        else{
            while ($row = mysqli_fetch_array($results)){
                $id = $row['search_id'];
                $title = $row['title'];
                $desc = $row['description'];
                $link = $row['link'];
                $img = '<img src="images/thumbnail/'.$row['search_id'].'.jpg" class="thumbnail">';

                $output .= '<div class="search_thumb">
                            <p class="search_cap"><a href="' . $link . '">' . $img . '<h3>' . $title . '</h3></a>' . $desc .
                            '<div class="clear"></div></p></div>';
            }
        }
    }
    else{
        header("location: ./");
    }
    print($output);
?>

<div class="row">

   </div>
<?php
        include('footer.html');

    ?>

</body>
</html>

1 个答案:

答案 0 :(得分:-1)

使用mysqli_num_rows()试试这个

<html !DOCTYPE HTML>
    <body>
<?php
    $title = "DenTEETH";
    include('header.html');
    //connect to database
    $db = mysqli_connect("127.0.0.1", "root", "", "authentication");


    if(isset($_GET['q']) && $_GET['q'] !== '')
    {
        $searchq = $_GET['q'];
        $sql = "SELECT * FROM search WHERE keyword LIKE '%$searchq%' OR title LIKE '%$searchq%'";
        $output='';
        $results = mysqli_query($db, $sql);

        if (mysqli_num_rows($results ) == 0){
            $output .= 'No search results for <b>"' . $searchq . '"</b>';
        }
        else{
            while ($row = mysqli_fetch_array($results)){
                $id = $row['search_id'];
                $title = $row['title'];
                $desc = $row['description'];
                $link = $row['link'];
                $img = '<img src="images/thumbnail/'.$row['search_id'].'.jpg" class="thumbnail">';

                $output .= '<div class="search_thumb">
                            <p class="search_cap"><a href="' . $link . '">' . $img . '<h3>' . $title . '</h3></a>' . $desc .
                            '<div class="clear"></div></p></div>';
            }
        }
    }
    else{
        header("location: ./");
    }
    print($output);
?>

<div class="row">

   </div>
<?php
        include('footer.html');

    ?>

</body>
</html>