duanfang5849 2013-12-12 00:08
浏览 39
已采纳

如何使用PHP和MySQL在搜索上显示“无结果”?

I would like when users run a search, it will search my DB. If it finds it, it outputs the table exactly the way I want. If there is no result, it just returns a blank page. How would I go about making it so that if no results are found, it displays something like "Sorry, no results." or something. I've tried using a counter such as $count = 0 and doing $count = $count + 1 in my while loop, and after the while, doing if ($count = 0) { echo "No results."}; but that just returned an error in the while loop.

$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);
  • 写回答

1条回答 默认 最新

  • doujiaohuo1096 2013-12-12 00:14
    关注

    You're probably getting an error because you're using mysqli_fetch_array instead of mysqli_fetch_assoc, otherwise the counter isn't a bad idea.

    You can also use num_rows to get a total count.

    edit: you should also look into using prepared statements if you're going to be doing db stuff with user input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单