This is how I currently do a query and return a dataset in CodeIgniter:
$sql = "SELECT `user_id`, `username`
FROM `users`
LIMIT 10";
$query = $this->db->query($sql);
$users = array();
foreach($query->result() => $row) {
$users[] = array(
'user_id' => $row->user_id,
'username' => $row->username
);
}
return $users;
As you can see I explicitly write what fields I want to return:
$users[] = array(
'user_id' => $row->user_id,
'username' => $row->username
);
Is there a way to have this be done automatically. So all fields that are selected in the sql query will be listed as the key and value of the array being returned?
Yes, there is. Use
result_array().You can find out more about this here.