I want to search a keyword with the use of the MySQL's LIKE
clause (for example: LIKE %$keyword%
) and then get the result back. If there are no results, i want to show some error message.
I have a table named ads_post
and i'm searching for the keyword in the fields named cat_name
and city_name
.
Could someone help me with this?
search-result.php
<?php
if (isset($_GET['submit'])) {
$keyword = $_GET['keywoed'];
$query = "select * from ads_post where cat_name like '%$keywoed%' or city_name like '%$keywoed%'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "cat Name: " . $row["cat_name"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
}
?>
HTML form code
<form action="search-result.php" method="get" >
<input class="form-control" name="keyword" placeholder="Search any keyword..?">
<input type="submit" value="search">
</form>