Php simple search engine?
I'm building a search engine that returns data and I'm having trouble displaying a message if there's no results in the database. I'm a beginner at MYSQLI. how would I do something like that? I tired counting the rows and if they're less then 0 to display it but I was having bugs while writing it.
Code:
<?php
$db = mysqli_connect('localhost','root', '', 'searchengine');
if(!$db) {
die('sorry we are having some problbems');
}
$searchTerm = mysqli_real_escape_string($db,$_GET['term']);
$searchTerm = trim($searchTerm);
if ($searchTerm == '') {
echo("no key words searched please try again");
}
else
{
$sql = mysqli_query(
$db,
sprintf(
"SELECT * FROM searchengine WHERE name LIKE '%s' LIMIT 0,20",
'%'. $searchTerm .'%'
)
);
while($ser = mysqli_fetch_array($sql)) {
echo "<a href='$ser[pageurl]'>$ser[img]</a>";
}
}
mysqli_close($db);
?>
Answer
Solution:
Use
mysqli_num_rows
...Answer
Solution:
since
mysqli_fetch_array()
returns null when there is no results, you can also putin the very end