Select only one column from multiple rows in codeigniter. How is this and is it more effective?

I think about efficiency, and I'm not sure about it anyway.

But I have a series of rows with multiple columns. I just need a name field from all the lines where a specific other key is a specific value. I can get all these lines as follows:

$this->db->where('res_id', $res_id);
$q = $this->db->get('products');
return $q->result();

then I can query through the array that it returns, and use only the name method for each liek object:

foreach($returned_value as $fun):
    echo $fun->name;
endforeach;

But I wonder if it would be more efficient to just select the name attribute from each row, and I feel stupid asking why I use active recording forever, but how would I do it. I understand that I can write it using a function $this->db->query(), but is there a way to specify it using the main active write commands? Thank.

+5
source share
1 answer
    $this->db->select('name'); 
    $this->db->from('tblNAME');   
    $this->db->where('res_id', $res_id);
    return $this->db->get()->result();

It is faster and more efficient, I suppose, because you are not returning everything.

UNTESTED

Here is a proven function that I use so you can find a good re sign

function get($id = null) 
        {
            $this->db->select('id, Username, First_Name, Last_Name, Role, Email');
            $this->db->from('user');
            if (!is_null($id)) $this->db->where('id', $id);
            $this->db->order_by('id', 'desc');
            return $this->db->get()->result();
        }
+6
source

All Articles