Welcome Guest, Not a member yet? Register   Sign In
ActiveRecord Question / Syntax Problem
#1

[eluser]Einspruch[/eluser]
This query should pull X Titles for SiteID Y, but instead it pulls X Titles for ALL SiteIDs. What am I doing wrong? I can get it working just fine using plain MySQL syntax, but not with ActiveRecord. Help!!! :-)

Code:
function bfg_top_100()
    {
        $this->db->select('Date');
        $this->db->select('Title');
        $this->db->select('Rank');
        $this->db->from('Portal_Rankings');
        $this->db->join('Sites', 'Portal_Rankings.SiteID = Sites.ID');
        $this->db->where('Portal_Rankings.SiteID', '1');
        $this->db->or_like('Title', 'Samantha');
        $this->db->or_like('Title', 'Luxor:');
        $this->db->or_like('Title', 'Midnight Mysteries:');
        $this->db->or_like('Title', 'Becky Brogan');
        $this->db->or_like('Title', 'Zombie Bowl-o-Rama');
        $this->db->or_like('Title', 'Square Logic');
        $this->db->order_by('Title');
        $this->db->order_by('Date', 'DESC');
        $query = $this->db->get();
        return ($query->num_rows() > 0)? $query->result() : FALSE;
    }
#2

[eluser]Ty Bex[/eluser]
Code:
function bfg_top_100()
    {
        $this->db->select('Portal_Rankings.Date');
        $this->db->select('Portal_Rankings.Title');
        $this->db->select('Portal_Rankings.Rank');
        $this->db->from('Portal_Rankings');
        $this->db->join('Sites', 'Portal_Rankings.SiteID = Sites.ID');
        $this->db->where('Portal_Rankings.SiteID', '1');
        $this->db->or_like('Portal_Rankings.Title', 'Samantha');
        $this->db->or_like('Portal_Rankings.Title', 'Luxor:');
        $this->db->or_like('Portal_Rankings.Title', 'Midnight Mysteries:');
        $this->db->or_like('Portal_Rankings.Title', 'Becky Brogan');
        $this->db->or_like('Portal_Rankings.Title', 'Zombie Bowl-o-Rama');
        $this->db->or_like('Portal_Rankings.Title', 'Square Logic');
        $this->db->order_by('Portal_Rankings.Title');
        $this->db->order_by('Portal_Rankings.Date', 'DESC');
        $query = $this->db->get();
        return ($query->num_rows() > 0)? $query->result() : FALSE;
    }

I am not sure if you are using sites or portal rankings but you need to define what table you are using the columns from.

I just posted a similar query at: http://ellislab.com/forums/viewthread/135430/
#3

[eluser]Einspruch[/eluser]
Thanks for your quick reply. Unfortunately that produces the same result. It retrieves records across more than one SiteID.
#4

[eluser]mah0001[/eluser]
Hi Einspruch,
After your query, add the line and you will know why you are not getting the results you want.
Code:
print $this->db->last_query();

You are building the query incorrectly. Using or_like will ignore your AND condition and you will get the results matching both your AND or any of the or_like conditions. What you need is to have parenthesis between your all ORs and your AND which cannot be achieved using AR.

If you want to use AR for building the query. replace your or_like statements with something like:
Code:
$where = "(title like'%Joe%' OR title like'%boss%' OR title like '%ative%')";//you need the parenthesis
$this->db->where($where);
#5

[eluser]Einspruch[/eluser]
That did the trick. Thank you! And that explains why I could get the right data using simple SQL syntax. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB