显示"结果未找到"如果没有结果数据库

时间:2016-11-10 15:11:53

标签: php mysql

我想显示一条消息"结果未找到"如果用户搜索未在数据库中退出的内容 这是我的代码,但它没有用。

有人可以告诉我我的代码有什么问题吗?

 <?php
        include 'database_conn.php';

        $eventCat = $_GET['catID'];
        $eventVenue =$_GET['venueName1'];

        $sqlSearch = " SELECT * FROM te_events 

                     WHERE catID = '$eventCat' AND venueID = '$eventVenue'";


        $rsSearch = mysqli_query($conn,$sqlSearch) or die(mysqli_error($conn));


    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    </head>
    <body>

        <thead>
        <th>Title</th> 
        </thead>
    <?php
    while ($row =mysqli_fetch_assoc($rsSearch)){
            //extract each field$
            $id         = $row  ["eventID"];
            $title      = $row  ["eventTitle"];
            $desc       = $row  ["eventDescription"];
            $venueId    = $row  ["venueID"];
            // $venue       = $row  ["venueName"];
            // $location   = $row  ["location"];
            $categoryId = $row  ["catID"];
            //$category   = $row  ["catDesc"];
            $eStart     = $row  ["eventStartDate"];
            $eEnd       = $row  ["eventEndDate"];
            $ePrice     = $row  ["eventPrice"]; 


        if(!empty($row)){

       //start a row
            echo"<tr>\n";
            //output the url
            echo"<td>\n";
            echo"<div><a href =\"allDetails.php?eventID=$id\">
                 $title</a></div>\n";
            echo"</td>\n";
        }
            else{
                echo "No event found! Please select other option.\n";



        }

        }
        mysqli_free_result($rsSearch);
        mysqli_close($conn);

    ?>

    </body>
    </html>

1 个答案:

答案 0 :(得分:2)

检查行数,然后根据结果编写代码。

$rows=mysqli_num_rows($rsSearch); 

if($rows > 0) {

while ($row = mysqli_fetch_assoc($rsSearch)){

// code for results

} 

} else {

// legend for no results

}

编辑:你不需要询问mysql结果数组是填充行还是空行,行本身永远不会为空,如果查询没有找到任何数据,它就不会存在于数组中。