Amin Amin - 1 year ago 66
PHP Question

Codeigniter Active Record issue (where and multiple 'AND' 'OR')

could somebody tell me how to generate the following query with codeigniter active record?

SELECT * FROM `tbl_admins` WHERE (`email`='' OR `uname`='user') AND `upass`='SecretePass';

not writing the query,
I mean passing different values through arrays...
or CI Active Record

thank you

Answer Source

I believe you can do this in a few ways. First, I am assuming you are passing in an array to the model function and that array is $filters.

Option One

$this->db->where('email', $filters["email"]);
$this->db->or_where('uname', $filters["email"]); 
$this->db->where('upass', $filters["password"]);

Option Two

$this->db->where("(email = '$filters["email"]' OR uname = '$filters["email"]') AND upass = '$filters["password"]'");

I did not test, so you may need to adjust the variables in option two's where a bit. But that is fairly close to what you want.

Here is another example of a similar Stack Overflow question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download