Wasim A.
Wasim A.

Reputation: 9890

Codeigniter WHERE AND OR together

I am trying to combine Where And OR in Codeigniter via code.

$this->db->where("id",1);
$this->db->where("status","live")->or_where("status","dead");

Appeared result is as query
where id=1 and status='live' OR status='dead'
Whereas i need result for this query.
where id=1 and (status='live' OR status='dead');

NOTE: Please don't give solution of string passed as parameter from where function. That's not fit into my code. I Just simplified my problem above.

Upvotes: 2

Views: 34207

Answers (3)

rial
rial

Reputation: 11

you just add ->group_start() and ->group_end()

$this->db->where("id",1);
$this->db->where("status","live")
->group_start()
->or_where("status","dead")
->group_end();

Upvotes: 1

PravinS
PravinS

Reputation: 2584

you can use where_in() like this

$status_array = array('live','dead');
$this->db->where("id",1);
$this->db->where_in('status', $status_array);

Upvotes: 9

Wasim A.
Wasim A.

Reputation: 9890

Just found solution, if it can help some one.

$this->db->where("id",1)->where("(status='live' OR status='dead')");

Upvotes: 12

Related Questions