简体   繁体   中英

How to display “No results” on search using PHP and 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);

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM