当使用sql LIKE找不到结果时回显一些东西

时间:2014-01-28 09:23:54

标签: php sql-like

我在我的网站上设了一个搜索框,搜索工作正常。但是当我输入一个在我的数据库中找不到的随机字符串(我正在搜索)时,会显示一个空白页面。如何显示“找不到匹配”之类的内容?这是我正在使用的代码:

$search = $_GET["zoek"];
$result = mysqli_query($con,"SELECT Afbeelding,Product,Prijs,Beschrijving FROM Producten WHERE Product LIKE '%$search%' order by Product ASC LIMIT 0, 5");
    echo '<table border="1px solid black" cellspacing="0" style="margin-top:47px"><tbody>';
    while($row = mysqli_fetch_array($result))
    { 
    echo "<tr>";
    echo "<td rowspan='2' width= '200'><img src='" . $row['Afbeelding'] . "' width= '200' height='250'></td>";
    echo "<td><b>" . $row['Product'] . "</b></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>" . $row['Beschrijving'] . "&nbsp;<i>Prijs: &euro;&nbsp;" . $row['Prijs'] . "</i><br/><br/></td>";
    echo "</tr>";

    }

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

3 个答案:

答案 0 :(得分:3)

请问,此查询返回的结果有多少mysqli_num_rows()

$result = mysqli_query($con,"SELECT Afbeelding,Product,Prijs,Beschrijving FROM Producten WHERE Product LIKE '%$search%' order by Product ASC LIMIT 0, 5");

if(mysqli_num_rows($result) > 0) {
    echo '<table border="1px solid black" cellspacing="0" style="margin-top:47px"><tbody>';
    while($row = mysqli_fetch_array($result))
    { 
    echo "<tr>";
    echo "<td rowspan='2' width= '200'><img src='" . $row['Afbeelding'] . "' width= '200' height='250'></td>";
    echo "<td><b>" . $row['Product'] . "</b></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>" . $row['Beschrijving'] . "&nbsp;<i>Prijs: &euro;&nbsp;" . $row['Prijs'] . "</i><br/><br/></td>";
    echo "</tr>";

    }

    echo "</tbody></table>";
} else {
    echo "No matches found";
}

答案 1 :(得分:0)

试试这个。

$search = $_GET["zoek"];
$result = mysqli_query($con,"SELECT Afbeelding,Product,Prijs,Beschrijving FROM Producten WHERE Product LIKE '%$search%' order by Product ASC LIMIT 0, 5");
 $row_cnt = mysqli_num_rows($result);
 if($row_cnt>0)
   {
    echo '<table border="1px solid black" cellspacing="0" style="margin-top:47px"><tbody>';
    while($row = mysqli_fetch_array($result))
    { 
    echo "<tr>";
    echo "<td rowspan='2' width= '200'><img src='" . $row['Afbeelding'] . "' width= '200' height='250'></td>";
    echo "<td><b>" . $row['Product'] . "</b></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>" . $row['Beschrijving'] . "&nbsp;<i>Prijs: &euro;&nbsp;" . $row['Prijs'] . "</i><br/><br/></td>";
    echo "</tr>";

    }

    echo "</tbody></table>";
    }
    else
    {
     echo "Sorry. No Results!";
    } 

答案 2 :(得分:0)

你应该试试这个。

$search = $_GET["zoek"];
    $result = mysqli_query($con,"SELECT Afbeelding,Product,Prijs,Beschrijving FROM Producten
    WHERE Product LIKE '%$search%' order by Product ASC LIMIT 0, 5");
    $total_rows=mysqli_fetch_row($result);
    if($total_rows>0){

        echo '<table border="1px solid black" cellspacing="0" style="margin-top:47px"><tbody>';
        while($row = mysqli_fetch_array($result))
        {
        echo "<tr>";
        echo "<td rowspan='2' width= '200'><img src='" . $row['Afbeelding'] . "' width= '200' height='250'></td>";
        echo "<td><b>" . $row['Product'] . "</b></td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>" . $row['Beschrijving'] . "&nbsp;<i>Prijs: &euro;&nbsp;" . $row['Prijs'] . "</i><br/><br/></td>";
        echo "</tr>";

        }

        echo "</tbody></table>";
    }
    else{
        echo "NO match found";
    }
Thanks