satya satya - 8 months ago 24
PHP Question

Can not search properly using Key word in PHP and MySQL

I need one help.I can not search the value from Database putting the key word using PHP and MySQL.I am explaining my code below.

$keyword = '%'.$searchKey.'%';
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '".$keyword."' and special='".$special_id."' and city='".$city_id."' and status=1 GROUP BY member_id ORDER BY member_id DESC ");
if(mysqli_num_rows($sql) > 0){
echo json_encode($result);

When i am calling the following URL to get the result.


Its giving me the following result.

"restaurant_name":"Sonoma on 9th"
"restaurant_name":"Jamesons Irish Pub 17th Ave. SW"
"restaurant_name":"Jamesons Irish Pub Brentwood NW"
"restaurant_name":"National on 17th"

Here my problem is i need to search the
Restaurant name
which contains the keyword
but here i am getting some other
Restaurant name
which doesn't contain keyword
.Here actually i need when user will search the restaurant name using keyword
it should give the restaurant name which contains that word
only not other restaurant name.Please help me.


you are using keyword "like" in your query which search "on" everywhere in restaurant name.

you should use spaces to make it specific

$keyword = '% '.$searchKey.' %';

check the spaces after first % sign and before the second one