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'] . " <i>Prijs: € " . $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'] . " <i>Prijs: € " . $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'] . " <i>Prijs: € " . $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'] . " <i>Prijs: € " . $row['Prijs'] . "</i><br/><br/></td>";
echo "</tr>";
}
echo "</tbody></table>";
}
else{
echo "NO match found";
}
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.