6

The users on the application are logged in by their e-mail. This e-mail is in a session. With this session I try to get more information of that user to print out there (sur)names and the avatar. When I do it in my navbar he's doing it all well but on the page itself it gives the following error: 'Fatal error: Cannot use object of type User as array'

What am i doing wrong?

PHP:

$user = new User();
$email = $_SESSION['email'];
$user->getUserInfoByEmail($email);
var_dump($user['avatar']);

FUNCTION:

public function getUserInfoByEmail($email)
{
    $db = new Db();
    $select = "SELECT * FROM tblusers WHERE email = '" . $_SESSION["email"] . "';";
    $result = $db->conn->query($select);
    return $data=$result->fetch_assoc();

}
1
  • 2
    The only error is in the var_dump, you are trying to access Object as an array. And now I see, that the answer was already added. So have a look at it and it's self-explanatory. Commented Apr 12, 2014 at 20:03

1 Answer 1

15

It is because you are using the $user object as an array (as the error message makes perfectly clear).

Change:

var_dump($user['avatar']);

To this:

var_dump($user->avatar);
Sign up to request clarification or add additional context in comments.

3 Comments

It's giving me 'null' as result.. but I don't get it. I'm doing exactly the same on the other place en that's working perfectly?
@MarnixVerhulst If it gives you null then maybe it is because it actually is null? It can also be because avatar does not exist in the object. Try to var_dump($user); and see if the avatar property is defined in the class.
Thanks for the tip... Its working now :) sorry i didn't check that

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.