簡體   English   中英

使用sql LIKE找不到結果時回顯某些內容

[英]echo something when no results where found using 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>";

只是問一下,該查詢通過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";
}

試試這個。

$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!";
    } 

您應該嘗試一下。

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM