返回一个数组从sql查询回显

时间:2013-01-31 12:55:03

标签: php sql echo

对于user_name,第二个echo正确打印,但是第一个只打印数组并给'数组到字符串错误'。

我的查询应该返回一个大约6个名字的数组。

是否有我遗漏的东西导致它无法正确返回数组或者我如何调整它以便我可以选择回显哪个$ single_name?

function get_all_names(){
    $this->db->select('username');
    $this->db->from('users');
    $Query = $this->db->get();
    $rows = $Query->result_array();
    return $rows;
}

 $all_names = array();
 $all_names = self::get_all_names();
 // $single_name = $all_names[1]; 
 // Above line did not work, I needed to add in ['username'] to it. 
 $single_name = $all_names[1]['username'];
 $user_name = ($_SESSION['username']);

 echo "<script type='text/javascript'> alert( '{$single_name }'); </script>";
 echo "<script type='text/javascript'> alert( '{$user_name }'); </script>";

1 个答案:

答案 0 :(得分:0)

更改

$single_name = $all_names[1] 

$single_name = $all_names[1]['username'];