SOLUTION ONE
1 2 3 4 5 6 |
$this->db->where('id', '3'); // here we select every column of the table $q = $this->db->get('my_users_table'); $data = $q->result_array(); echo($data[0]['age']); |
SOLUTION TWO
1 2 3 4 5 6 7 |
// here we select just the age column $this->db->select('age'); $this->db->where('id', '3'); $q = $this->db->get('my_users_table'); $data = $q->result_array(); echo($data[0]['age']); |
SOLUTION THREE
1 2 3 4 5 6 7 |
$this->db->select('age'); $this->db->where('id', '3'); $q = $this->db->get('my_users_table'); // if id is unique, we want to return just one row $data = array_shift($q->result_array()); echo($data['age']); |
SOLUTION FOUR (NO ACTIVE RECORD)
1 2 3 |
$q = $this->db->query('SELECT age FROM my_users_table WHERE id = ?',array(3)); $data = array_shift($q->result_array()); echo($data['age']); |
you can use row() instead of result()
1 2 |
$this->db->where('id', '3'); $q = $this->db->get('my_users_table')->row(); |
Accessing a single row
1 2 3 4 5 6 7 |
//Result as an Object $result = $this->db->select('age')->from('my_users_table')->where('id', '3')->limit(1)->get()->row(); echo $result->age; //Result as an Array $result = $this->db->select('age')->from('my_users_table')->where('id', '3')->limit(1)->get()->row_array(); echo $result['age']; |