View Single Post
Old 02-22-2017, 09:39 PM   #1
alexwaston
Registered User
 
Join Date: Aug 2016
Posts: 4
Select query - ignore search criteria if value is empty

Hi, I have a simple database that I am querying from PHP. I have a user input some constraints in via a form, and then want to return results from a SELECT query based on the constraints. What I have is working when both constraints are used. However, if one of the constraints is not specified by the user, the SELECT query is returning no results, which I think is because it seeing the constraint variable as empty.

How do I get the SELECT query to use the constraint if it is not empty, but to ignore it if it is empty?

Here is what I tried earlier but it isn't working. I thought I could test for empty and use OR to ignore it.

$query="SELECT * FROM food_items
WHERE (type IN ('$foodtypes') OR ('$foodtypes') > '')
AND ('$budget' > '' OR cost <= ('$budget'))
";

In this example, type and cost are fields in the database and $foodtypes and $budget are variables taken from the user inputs.

Thanks

Relax: popular ringtones, hip hop ringtones, rap ringtones

Last edited by alexwaston; 03-07-2017 at 07:54 PM..
alexwaston is offline   Reply With Quote