Welcome Guest, Not a member yet? Register   Sign In
Active Record: WHERE(.. OR ..) AND ..
#1

[eluser]Buso[/eluser]
I need this:

Code:
SELECT * FROM table WHERE id = ? AND (status = ? OR ip = ?);

Is it possible to do it with active record?

This is what I have so far:

Code:
$this->db->where('id',$id);
$this->db->where('status',$status);
$this->db->or_where('ip',$ip);

But it's not the same
#2

[eluser]wh1tel1te[/eluser]
Code:
$this->db->where('id',$id);
$this->db->where("(status = '" . $status . "' OR ip = '" . $ip . "')");
#3

[eluser]Buso[/eluser]
thanks =)
#4

[eluser]wowdezign[/eluser]
This kind of stuff is exactly why I am starting to use more and more query binding.

Your query above is almost already written to bind:

Code:
$sql="SELECT * FROM table WHERE id = ? AND (status = ? OR ip = ?)";
$bind =  array($id,$status,$ip);
$this->db->get($sql,$bind);

If you aren't passing in variables, I've had success with:

Code:
$sql="SELECT * FROM table WHERE id = 3 AND (status = 'somstatus' OR ip = 'x.x.x.x')";
$bind =  array();
$this->db->get($sql,$bind);

I used to love active record. But, when I have a complex query, it's too hard for me to convert it. Since binding does the same thing, I just use it. :-)

It's mentioned at the very bottom of this page in the manual:

http://ellislab.com/codeigniter/user-gui...eries.html
#5

[eluser]Buso[/eluser]
[quote author="wowdezign" date="1257910797"]This kind of stuff is exactly why I am starting to use more and more query binding.

Your query above is almost already written to bind:

Code:
$sql="SELECT * FROM table WHERE id = ? AND (status = ? OR ip = ?)";
$bind =  array($id,$status,$ip);
$this->db->get($sql,$bind);

If you aren't passing in variables, I've had success with:

Code:
$sql="SELECT * FROM table WHERE id = 3 AND (status = 'somstatus' OR ip = 'x.x.x.x')";
$bind =  array();
$this->db->get($sql,$bind);

I used to love active record. But, when I have a complex query, it's too hard for me to convert it. Since binding does the same thing, I just use it. :-)[/quote]
yes I think im gonna stick to binding too..
active record has just lost its magic to me =(
thanks




Theme © iAndrew 2016 - Forum software by © MyBB