I am trying to use multiple search options where user can select multiple options to filter records.
When user don't select filter option(s), by default all records should be returned.
Here's my SQL query :
$query =
sprintf("SELECT * FROM users_leave_request
where leave_from >= '$leave_from'
AND leave_to <= '$leave_to'
AND leave_status = '$leave_stat'
AND user_id = '$emp_id'");
$result=$this->db->exec($query);
What I intend to do is that:
- Suppose
$leave_stat
parameter is empty, then records for allleave_stat
values should be returned. - Similarly if
$emp_id
is empty, records for all users should be returned.
It's somewhat like disabling that *extra AND*
where condition when parameter is empty.
Can I do this with a single query or do I have to use separate queries for that? Any help is very much appreciated. Thanks.