[英]echo something when no results where found using sql LIKE
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() : 只是问一下,该查询通过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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.