2

The second echo, for user_name, prints correctly, but the first will only print array and give 'array to string error'.

My query should return an array of about 6 names.

Is there something I'm missing that is causing it not to be returning an array correctly or how can I tweak this so I can choose which $single_name to echo?

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>";
7
  • 2
    print the result using print_r($single_name) and check whether the result is an array or simple value? Commented Jan 31, 2013 at 13:03
  • 1
    i have made an edit to code. $all_signums is now $all_names Commented Jan 31, 2013 at 13:03
  • 1
    Does it work now? What do you get when you use var_dump($all_names); Commented Jan 31, 2013 at 13:03
  • 1
    Check this link it might help you stackoverflow.com/questions/11510398/… Commented Jan 31, 2013 at 13:13
  • 1
    FIXED, I needed to change the line - $single_name = $all_names[1]; TO $single_name = $all_names[1]['username']; Thanks for your help guys Commented Jan 31, 2013 at 14:24

1 Answer 1

0

Change

$single_name = $all_names[1] 

to

$single_name = $all_names[1]['username'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.