Welcome Guest, Not a member yet? Register   Sign In
CI Query --help required
#1

[eluser]Computerzworld[/eluser]
Hello,
I am having one query in which there is one or condition & one and condition. Here is the format of my query.

Code:
$this->db->where('field1',$this->field1);
$this->db->or_where('field2',$this->field2);
$this->db->where('field3',$this->field3);
$query = $this->db->get('users');

And if I echo this query it gives result something like this.

Code:
$query = "select * from users where field1 = '".$this->field1."'" or field2 = '".$this->field2."' and field3 = '".$this->field3."'";

And hence it contains no brackets between or conditions , and condition is neglected. Is there anyway so that i can have brackets outside the query in CI format so that or condition is grouped alongwith and condition something like this...

Code:
$query = "select * from users where (field1 = '".$this->field1."'" or field2 = '".$this->field2."') and field3 = '".$this->field3."'";

How can I write such query using CI? Please help me. Thanks in advance....
#2

[eluser]sybrex[/eluser]
As far as I know current version of CI doesn’t support brackets.
#3

[eluser]Computerzworld[/eluser]
that means i have to write custom query using $this->db->query() . right?
#4

[eluser]Colin Williams[/eluser]
The ActiveRecord class is limited in this regard. Your best bet is to pass a custom string

Code:
$this->db->where('x = z AND (y = s OR y = r)', NULL, FALSE);

You might, however, want to manually escape ($this->db->escape()) any variables you intend to use in the string.
#5

[eluser]TheFuzzy0ne[/eluser]
Not at all.

Code:
//Second parameter = NULL, third parameter prevents escaping.
$this->db->where("(field1 = '".$this->db->escape($this->field1)."' or field2 = '".$this->db->escape($this->field2)."')", NULL, FALSE);
$this->db->where("field3", $this->field3);

EDIT: Edited slightly for clarity
#6

[eluser]Colin Williams[/eluser]
Haha! good call, FuzzyOne.
#7

[eluser]Computerzworld[/eluser]
yes that is ultimately the customization of where condition....
thanks for reply....




Theme © iAndrew 2016 - Forum software by © MyBB