使用codeigniter从数据库返回两行

问题描述:

This query works as I want but only returns one value.

public function get_doctor_name($doctor_id)  {
  $result = $this->db->query("SELECT first_name, last_name from doctors where id= $municipio_id")->row_array();
  return $result['first_name'];
}

How to return the last_name as I did for first_name in the same query?

此查询按我的意思工作,但只返回一个值。 p>

  public function get_doctor_name($ doctor_id){
 $ result = $ this-> db-> query(“SELECT first_name,last_name from doctors where id = $ municipio_id”) - > row_array(); 
 return  $ result ['first_name']; 
} 
  code>  pre> 
 
 

如何像 first_name一样返回 last_name code> 代码>在同一个查询中? p> div>

Your query is actually correct.

However, you can use return $result->result_array(); to get an array of all fields, first_name, last_name etc.

You can return array:

return $result;

You can return a string, for example:

return $result['first_name'] . ' ' . $result['last_name'];

You can return array with specific keys that you define:

return [
    'f_name' => $result['first_name'], 
    'l_name' => $result['last_name']
];