I have a list of products and I am trying to build a search query for. The product row has a field called 'approved' which is set to '1' if approved or '0' if not approved. I only want to search approved products set to '1'. Here is my code so far: SELECT * FROM archive WHERE approved='1' and products LIKE '%$search%' or description like '%$search%' LIMIT $offset, $entries_per_page ") or die("Selecting that crap failed "); Code (markup): $search is of course a variable. Any way so far this code selects all products that match $search despite what approved is set at. Can someone help me formulate this query to only select the ones that match $search where approved='1' ? Thanks in advance!
Try putting all "OR"s between parenthesis. SELECT * FROM archive WHERE approved = 1 AND ( products LIKE '%$search%' OR description LIKE '%$search%' ) LIMIT $offset, $entries_per_page Code (sql):
The issue may be related to the case of the text in the name & description fields and your search string. You could try using ilike, or make sure everything is always upper or always lower: $search = strtolower($search) lower(name) like '%$search%'