• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is session->userdata supposed to return an array?

#1
[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'?

#2
[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?

#3
[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?

#4
[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;
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.