phpcodeigniteractiverecord

CodeIgniter active record querying error: "Fatal error: Uncaught Error: Call to undefined method CI_DB_query_builder::result()"


Can someone tell me how to write this properly?

function get_tech()
{
    $this->db->select('u.id
                      ,u.first_name
                      ,us.id
                      ,us.group_id');
    $this->db->from('users u');
    $this->db->join('users_groups us', 'us.id = u.id', 'left');
    $records = $this->db->where('us.group_id', '3');
    $data = array();
    foreach ($records->result() as $row) {
        $data[$row->id] = $row->first_name;
    }
    return ($data);
}

I'm trying to populate a drop down menu using an array, but I need to only grab users that are part of users_group/group_id = 3.

Therefore, in my very limited knowledge, I'm needing:

select X from Users LEFT JOIN users_groups WHERE group_ID = 3

Solution

  • You need to call $this->db->get() in order to actually run your query.

    function get_tech() {
        $this->db->select('u.id
                          ,u.first_name
                          ,us.id
                          ,us.group_id');
        $this->db->from('users u');
        $this->db->join('users_groups us','us.id = u.id','left');
        $this->db->where('us.group_id', '3');
        $records = $this->db->get();
    
        $data = array();
        foreach($records->result() as $row){
            $data[$row->id] = $row->first_name;
        }
        return $data;
    }