1

I'm trying to query a mongodb database with the _id stored in a session variable...

getting the _id and store as session variable:

$_SESSION['user']['userid']=$user['_id']->{'$id'};

querying db - error on this line:

$user=collection->findOne(array('_id' => new MongoId($_SESSION['user']['userid'])));

can't figure it out.

0

2 Answers 2

2

Should be $user=$collection->findOne(array('_id' => new MongoId($_SESSION['user']['userid']))); You missed a $ infront of collection

Sign up to request clarification or add additional context in comments.

4 Comments

Now I get a Notice: array to string conversion
Are you getting that on your echo statement ?
Replace echo with print_r. That is because an array can't be echoed all together.
You need to json_encode the array and then pass it off.
0
<?php

$users= $mongo->my_db->users;
$user = $users->findOne(array('_id' => new MongoId($_SESSION['user']['userid']));
print_r($user);

?>

Comments

Your Answer

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