Tony
Tony

Reputation: 287

Codeigniter AND OR AND

It is posible to do: (condition 1 AND condition2) OR (condition3 AND condition4) in codigniter?

I try:

$this->db->where(condition1 - condition2);
$this->db->or_where(condition3 - condition4);

But this is: (condition1 AND condition2) OR (condition3 OR condition4)

Upvotes: 2

Views: 6972

Answers (6)

Raham
Raham

Reputation: 4951

If you are using PHP5 then the following code will fix your problem.

$this->db->select("*")
->where("condition_1 && condition_2")
->or_where("condition_3 && condition_4");

Or use the following code will also works.

$this->db->where("condition_1 ");
$this->db->where("condition_2");
$this->db->or_where("condition_3 "); 
$this->db->where("condition_4 "); 

Upvotes: 0

Needhi Agrawal
Needhi Agrawal

Reputation: 1326

yes it is possible,for convenience take whatever conditions you want to used into one variable

$where ="(condition 1 AND condition2) OR (condition3 AND condition4)";
$this->db->where($where);

Upvotes: 3

Raffaele Izzia
Raffaele Izzia

Reputation: 242

$first="(COND1 AND COND2)";
$second= "(COND3 AND COND4)";

$query=$this->db->where($first)->or_where($second)->get();

Upvotes: 1

Michael Benjamin
Michael Benjamin

Reputation: 2915

Just use a normal query like so:

$query="SELECT *
FROM table1 INNER JOIN table2 on table1.col=table2.col
WHERE (table1.whatever=? AND table1.hello=?) OR (table2.foo=? AND table2.pizza=?)
LIMIT 1";
$params=array();
$params[]='whatever';
$params[]='world';
$params[]='bar';
$params[]='cheese';


$result=$this->db->query($query,$params);
$result=$result->result_array();
print_r($result);

Upvotes: 0

Vimal Patel
Vimal Patel

Reputation: 607

Have you try like this :::::?

$this->db->where(COND1);
$this->db->where(COND2);
$this->db->or_where(COND3); 
$this->db->where(COND4); 

Upvotes: 1

Cody Covey
Cody Covey

Reputation: 1060

Just write it out in your where

$this->db->where("(condition1 AND condition2) OR (condition3 AND condition4)");

this will still escape data and you don't have to fool with the or_where method.

Upvotes: 5

Related Questions