简体   繁体   中英

Access Array in Session Codeigniter

My Model :

Class Data_Anggota extends CI_Model
{
    function login($username, $password)
    {
        $this -> db -> select('*');
        $this -> db -> from('data_anggota');
        $this -> db -> where('email', $username);
        $this -> db -> where('password', $password);
        $this -> db -> limit(1);

        $query = $this -> db -> get();

        if($query -> num_rows() == 1)
        {
            return $query->row();
        }
        else
        {
            return false;
        }
    }
}

My Controller :

$result = $this->data_anggota->login($username, $password);

$sess_array = array(
    'id' => $result->id_anggota,
    'username' => $result->email,
    'name' => $result->nama
);
$this->session->set_userdata('GIuser_login', $sess_array);

....

$data['profile'] = $this-> session -> userdata('GIuser_login');
$this->load->view('profil_view', $data);

at my profile_view :

echo $profile->name;
echo $profile;

both error and show this message

Message: Array to string conversion

Message: Trying to get property of non-object

$sess_array = array(
    'id' => $result->id_anggota,
    'username' => $result->email,
    'name' => $result->nama
);
$this->session->set_userdata('GIuser_login', $sess_array);

Because you have set session user info as an array, you need to change your profile_view

from

echo $profile->name;
echo $profile

to

echo $profile['name'];
echo $profile['username'];

Give it a go and it should work.

You are accessing it as object, but it is an array so you can access as below,

echo $profile['name'];
echo $profile['username'];

Or by Object ,

$profile = (object)$profile;
print_r($profile);
echo $profile->name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM