Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Cannot use object of type User as array

Tags:

function

php

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();

}
like image 963
Marnix Verhulst Avatar asked Dec 12 '22 06:12

Marnix Verhulst


1 Answers

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);
like image 55
Sverri M. Olsen Avatar answered Dec 13 '22 21:12

Sverri M. Olsen