【问题标题】:How to display "No results" on search using PHP and MySQL?如何使用 PHP 和 MySQL 在搜索中显示“无结果”?
【发布时间】:2013-12-12 00:08:41
【问题描述】:

我希望当用户运行搜索时,它会搜索我的数据库。如果找到它,它将完全按照我想要的方式输出表格。如果没有结果,它只是返回一个空白页。我将如何制作它,以便如果没有找到结果,它会显示类似“对不起,没有结果”的内容。或者其他的东西。我尝试使用诸如 $count = 0 之类的计数器并在我的 while 循环中执行 $count = $count + 1 ,然后在 while 之后执行 if ($count = 0) { echo "No results."};但这只是在 while 循环中返回了一个错误。

$result = mysqli_query($con,"SELECT * FROM equipmentt WHERE title like '%$search%' or description like '%$search%' or incondition like '%$search%' or assetnumber like '%$search%' or manufacturer like '%$search%' or model like '%$search%' or yearof like '%$search%' or serialnumber like '%$search%' or dimensions like '%$search%' or speed like '%$search%' or electricity like '%$search%' or shippingweight like '%$search%' or status like '%$search%'");


while($row = mysqli_fetch_array($result))
{
 echo "<table cellpadding='0' cellspacing='0' id='equiptable'>
<tr><td>
<table><tr><td id='eqimg'><img src='" . $row['img'] . "'></td><td>
<table id='altrow' cellpadding='0' cellspacing='0'>
<tr><td colspan='2' id='eqtitle'>" . $row['title'] . "</td></tr>
<tr><td class='b'>Description</td><td>" . $row['description'] . "</td></tr>
<tr><td class='b'>Condition</td><td>" . $row['incondition'] . "</td></tr>
<tr><td class='b'>Asset Number</td><td>" . $row['assetnumber'] . "</td></tr>
<tr><td class='b'>Manufacturer</td><td>" . $row['manufacturer'] . "</td></tr>
<tr><td class='b'>Model</td><td>" . $row['model'] . "</td></tr>
<tr><td class='b'>Status</td><td class='" . $row['status'] . "'>" . $row['status'] . "</td></tr>
<tr><td class='b'>Year</td><td>" . $row['yearof'] . "</td></tr>
<tr><td class='b'>Serial Number</td><td>" . $row['serialnumber'] . "</td></tr>
<tr><td class='b'>Dimensions</td><td>" . $row['dimensions'] . "</td></tr>
<tr><td class='b'>Speed</td><td>" . $row['speed'] . "</td></tr>
<tr><td class='b'>Electricity</td><td>" . $row['electricity'] . "</td></tr>
<tr><td class='b'>Shipping Weight</td><td>" . $row['shippingweight'] . "</td></tr>
</table>
</td></tr>
</table>
<p>";
}

mysqli_close($con);

【问题讨论】:

    标签: php mysql search


    【解决方案1】:

    您可能会遇到错误,因为您使用的是 mysqli_fetch_array 而不是 mysqli_fetch_assoc, 否则计数器不是一个坏主意。

    您也可以使用num_rows 来获取总数。

    编辑:如果您要使用用户输入进行数据库操作,您还应该考虑使用prepared statements

    【讨论】:

    • 我使用了 num_rows 并将其设置为计数。然后使用 if ($count == 0) {echo "No results.";} 非常感谢您!另外,我目前正在研究 mysqli_fetch_assoc,所以谢谢你的建议!
    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多