문제

I made a search box on my website and searching works fine. But when I type in a random string that can't be found in my database (in which I'm searching) a blank page is displayed. How can I display something like "no matches found" instead? This is the code I'm using:

$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>";
도움이 되었습니까?

해결책

Just ask, how many results this query returns with 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";
}

다른 팁

Try with this.

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

You should try this.

$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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top