How to use OR_Where with AND condition in CodeIgniter?

3.9k Views Asked by At

I am using CodeIgniter. I need to understand how where and or_where condition working in codeigniter query builder.

I need an output like where a="" AND b="" AND c="" OR d="" AND e=""

so I tried an associative array.

$where= array('a'=>$firstname,'b'=>$lastname,'c'=>1);
$or_where= array('d' =>$customer_mobile,'e'=>1);

But I am getting the output where a="" AND b="" AND c="" OR d="" OR e=""

Would you help in out in this?

Yes, My question is different. I asked about the or_where condition. Please check my expect output and getting output. And the duplicate question is different.

1

There are 1 best solutions below

3
On BEST ANSWER

Hope this will help you :

Use query grouping like this : change your table name and variables according to you

$firstname = 'a';
$lastname = 'b';
$customer_mobile = 'd';
$this->db->select('*')->from('users')
        ->group_start()
                ->where('a', $firstname)
                ->where('b', $lastname)
                ->where('c', '1')
                ->or_group_start()
                        ->where('d', $customer_mobile)
                        ->where('e', '1')
                ->group_end()
        ->group_end()
->get();
echo $this->db->last_query();

The output will be like this :

SELECT * FROM `users` 
WHERE ( `a` = 'a' AND `b` = 'b' AND `c` = '1'
OR ( `d` = 'd' AND `e` = '1' ) )

For more : https://www.codeigniter.com/user_guide/database/query_builder.html#query-grouping