Welcome Guest, Not a member yet? Register   Sign In
Mysql Queries - Best Tips

So I have multiple tables that I need to pull the data from. This is how I have been doing it but I not confident that this is the best way to do this. here is some code

function getBlogs()
        $query = $this->db->get('blogs');
        $result = $query->result_array();
        foreach($result as $i => $post){
            $result[$i]['blogger'] = $this->getBlogger($post['author_id']);
            $result[$i]['categories'] = $this->getCategory($post['cat_id']);
        return $result;
    function getBlogger($author_id)
         $query = $this->db->get('accounts');
         return $query->row_array();
    function getCategory($cat_id)
         $query = $this->db->get('posts_categories');
         return $query->result_array();

[eluser]Armchair Samurai[/eluser]
Assuming there is a 1-to-1 relationship between the tables, just join them rather than making three separate queries.
$this->db->join('accounts y', 'x.author_id = y.id');
$this->db->join('posts_categories z', 'x.cat_id = z.post_id');
$query = $this->db->get('blogs x');
return $query->result_array();

Theme © iAndrew 2016 - Forum software by © MyBB