如果mysql中不存在任何记录,则不显示

时间:2014-11-30 09:16:52

标签: php mysql

我有这个表单来搜索mysql数据库中的名称

<form action="search.php" method="GET">
<input type="text" placeholder="Search" name="name">
<input type="submit" value="Search">

这是search.php

<?php
        name = $_GET['name'];

        require_once("connect.php");

        $records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");

        echo 
        "<table>
        <thead>
        <tr>
        <th>Name</th> 
        <th>Email</th>
        <th>Description</th>            
        </tr>
        </thead>
        <tbody>";

        if (mysqli_num_rows($records)== 0){
              echo "No data available for that name specified";
            }
            else {
              while($row=mysqli_fetch_array($records)) {

            $name = $row['Name'];
            $email = $row['Email'];
            $desc = $row['Desc']; 

            echo 
            "<tr>
            <td>".$name."</td>
            <td>".$email."</td>   
            <td>".$desc."</td>                                     
            </tr>";

           }

        }

        echo    
        "</tbody>
        </table>";
?>

所以当我搜索正确显示的数据库中存在的名称时没有问题,但是当我搜索数据库中不存在的名称时出现问题..我希望它只显示<登记/> "No data available for that name specified"输出,但我也会在输出中看到空表------------&gt; IMAGE ..

那么如何摆脱输出的空表呢?

4 个答案:

答案 0 :(得分:1)

如果在桌子外面,请尽快说明。

<?php
    name = $_GET['name'];

    require_once("connect.php");

    $records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");

    if (mysqli_num_rows($records)== 0){
       echo "No data available for that name specified";
    } else {


    echo 
    "<table>
    <thead>
    <tr>
    <th>Name</th> 
    <th>Email</th>
    <th>Desc</th>            
    </tr>
    </thead>
    <tbody>";


          while($row=mysqli_fetch_array($records)) {

        $name = $row['Name'];
        $email = $row['Email'];
        $desc = $row['Desc']; 

        echo 
        "<tr>
        <td>".$name."</td>
        <td>".$email."</td>   
        <td>".$desc."</td>                                     
        </tr>";

       }



    echo    
    "</tbody>
    </table>";  
   }
?>

答案 1 :(得分:1)

更改您的if子句,如下所示并记住添加exit()die()函数,如果数据库中没有任何数据,这将结束您的php,如果有的话然后将开始创建一次表并重复填充数据库中给定行数据的表行。

if (mysqli_num_rows($records)== 0){
    echo "No data available for that name specified";
    exit();
} else {
    echo 
    "<table>
    <thead>
    <tr>
    <th>Name</th> 
    <th>Email</th>
    <th>Desc</th>            
    </tr>
    </thead>
    <tbody>";

    while($row=mysqli_fetch_array($records)) {

        $name = $row['Name'];
        $email = $row['Email'];
        $desc = $row['Desc']; 

        echo 
        "<tr>
        <td>".$name."</td>
        <td>".$email."</td>   
        <td>".$desc."</td>                                     
        </tr>";

      }

    echo    
    "</tbody>
    </table>";
}

答案 2 :(得分:0)

移动echo "<table> <thead> <tr> <th>Name</th> <th>Email</th> <th>Desc</th>
</tr> </thead> <tbody>";

进入if语句。这样它只会在数据可用时显示表格!

答案 3 :(得分:0)

        $name = $_GET['name'];

        require_once("connect.php");

        $records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");

if (mysqli_num_rows($records)== 0){
    echo "No data available for that name specified";
}
else {
    echo
    "<table>
        <thead>
        <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Desc</th>
        </tr>
        </thead>
        <tbody>";


    while ($row = mysqli_fetch_array($records)) {

        $name = $row['Name'];
        $email = $row['Email'];
        $desc = $row['Desc'];

        echo
            "<tr>
            <td>" . $name . "</td>
            <td>" . $email . "</td>
            <td>" . $desc . "</td>
            </tr>";

    }
    echo "</tbody></table>";
}

试试这个。但不要忘记逃避$ _GET ['name'],如htmlspecialchars和real_escape_string