Welcome Guest, Not a member yet? Register   Sign In
Simple MySQL question....
#1

[eluser]codelearn[/eluser]
Hey guys,

Struggling with a pretty simple mysql query. I'm trying to do a query which utilizes the orwhere functionality of Active Record. I need to retrieve the rows in which name = "test" and tag_1, tag_2 or tag_3 = "1".

What I'm trying now:
Code:
$this->db->where('name','test');
$this->db->orwhere('tag_1',"1");
$this->db->orwhere('tag_2',"1");
$this->db->orwhere('tag_3',"1");

The orwhere part of it works, its the first where statement which should have an AND after it that is ignored.

This is another example of the same type of thing I am trying to do that does not work:
Code:
$this->db->query("SELECT * FROM podcasts WHERE tag_1 = '$tag_id' OR tag_2 = '$tag_id' OR tag_3 = '$tag_id' OR tag_4 = '$tag_id' OR tag_5 = '$tag_id' AND is_active = '1' ORDER BY date DESC LIMIT $offset, $num");

Anyone know the right way to do this?

Thanks!!
#2

[eluser]SeanJA[/eluser]
http://ellislab.com/codeigniter/user-gui...tml#select

Code:
$this->db->get_where(array('name' => $test))

Wink

You are loading the database library right?

Code:
$this->load->database();
#3

[eluser]codelearn[/eluser]
Umm... I'm not sure what this has to do with the OR question? Am I missing something?
#4

[eluser]Armchair Samurai[/eluser]
Sounds like you need parenthesis, which is currently unsupported in AR, so you need to do it the hard way:
Code:
$this->db->where('name', 'test');
$this->db->where('(`tag_1` = 1 OR `tag_2` = 1 OR `tag_3` = 1)', NULL, FALSE);
#5

[eluser]SeanJA[/eluser]
Maybe I misunderstood the question...

You said that

Code:
$this->db->query("SELECT * FROM podcasts WHERE tag_1 = '$tag_id' OR tag_2 = '$tag_id' OR tag_3 = '$tag_id' OR tag_4 = '$tag_id' OR tag_5 = '$tag_id' AND is_active = '1' ORDER BY date DESC LIMIT $offset, $num");

didn't work, so I assumed that the query was not working at all, instead of producing the incorrect results. My bad I guess, but in my defense, there are no brackets there...

Code:
$this->db->query("SELECT * FROM db WHERE something = 'foo' AND (foo = $tag1 OR foo2 = $tag2)")

is what you meant then?
#6

[eluser]codelearn[/eluser]
Yes!

Thanks fellas.




Theme © iAndrew 2016 - Forum software by © MyBB