Search code examples
phpcodeigniterselectactiverecordsum

what's wrong with my sum query in model php codeigniter?


here's my model code :

function total($id)
    {
        $this->db->select_sum('score');
        $q = $this->db->get('my_table');
        $this->db->where('id',$id);
        $this->db->group_by('id');
        return $q->row()->score;  

    }  

why the output still sum all of row not the specific row with id?


Solution

  • $this->db->get() actually runs the query. You need to call that last.

    function total($id)
        {
            $this->db->select_sum('score');
            $this->db->where('id',$id);
            $this->db->group_by('id');
    
            $q = $this->db->get('my_table');
            return $q->row()->score;  
    
        }