简体   繁体   中英

Where clause in PHP MySQL not working with Like keyword

I have been trying to filter my data according to the c_type = "Engineering' and search keyword in the search box. The searching part is working fine but the query is not working it displays the whole result.

This is for filtering my result

$query1 = mysqli_query($conn, "SELECT * FROM col_details WHERE c_type = 'Engineering' and c_name LIKE '%{$name}%' OR location LIKE '%{$name}%' ");

I am trying to display all the results in my col_details table with c_type is Engineering and c_name or location is taken from the Search Box. The search box code is working fine but it's displaying all data and not the data with c_type = 'Engineering'.

Any help appreciated.

Thanks.

Your OR clause should be wrapped in brackets, Try this:

$query1 = mysqli_query($conn, "SELECT * FROM col_details WHERE c_type = 'Engineering' and (c_name LIKE '%{$name}%' OR location LIKE '%{$name}%') ");

Hope it helps.

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