CodeIgniter Forums
Is session->userdata supposed to return an array? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Is session->userdata supposed to return an array? (/showthread.php?tid=33167)



Is session->userdata supposed to return an array? - El Forum - 08-17-2010

[eluser]Unknown[/eluser]
Code:
$id = 1;
$custom_session_data = array('id' => $id);
$this->session->set_userdata($custom_session_data);
$id = $this->session->userdata('id');
print_r($id);
Array ( [0] => stdClass Object ( [id] => 1 ) )
I was hoping to get the data as int/str. Am I doing something wrong or this is supposed to be like this? If yes, then is there a way to extract the wanted int without getting 'messy'?


Is session->userdata supposed to return an array? - El Forum - 08-18-2010

[eluser]WanWizard[/eluser]
If I use that same code here, I get "int 1" returned.

The CI session library doesn't convert data, and it certainly doesn't create objects. Are you using a custom session library?


Is session->userdata supposed to return an array? - El Forum - 08-18-2010

[eluser]Unknown[/eluser]
No custom library. Just the default one, autoloaded.

Oh my, I've given false information in the first post. Apparently the array originates straight from the database.
Code:
$query = $this->db->query("SELECT id FROM users WHERE user = 'asd'");
$id = $query->result();

I'm brand new to CI, so I might be oblivious about correct syntax etc. Any ideas?


Is session->userdata supposed to return an array? - El Forum - 08-18-2010

[eluser]WanWizard[/eluser]
See the user guide.

$query->result() returns an array of objects, one for each record in the resultset. You should be doing something like
Code:
$query = $this->db->query("SELECT id FROM users WHERE user = 'asd'");

if ($query->num_rows() > 0)
{
    $row = $query->row();
    $id = $row->id;
}
else
{
    $id = FALSE;
}