I'm implementing a search function on my web page. The user will be able to enter a search string, but also to specify some search criteria from a dropdown menu. My question is the following: If the user doesn't specify any search criteria (leaves it empty) then I want the search to disregard it.
As the code stands, if the user left category empty MySQL would look for a product with the category="", how can I change that to category=ALL. I don't want to do it with PHP!!
SELECT
url
FROM
product
WHERE
name LIKE '$new_search AND
(CHAR_LENGTH('$category') > 0 AND category = '$category') OR
(CHAR_LENGTH('$category') = 0 AND category = 'ALL')
If you've got a huge database though, that could increase quite a bit of overhead. Why don't you want to do it in PHP - it's real easy:
you cannot really make if condition in sql statement. can you tell us the reason why you want to that in sql statement?
this is an extension of pickle model supposed to be the easiest way to do it.