Fatal error: Cannot use object of type User as arr

2020-04-08 10:55发布

问题:

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:

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


标签: php function