致命错误:不能将用户类型的对象用作数组 [英] Fatal error: Cannot use object of type User as array
问题描述
应用程序上的用户通过其电子邮件登录.该电子邮件处于会话中.在本次会议上,我尝试获取有关该用户的更多信息,以在其中打印出(姓)名称和头像.当我在导航栏中执行此操作时,他做得很好,但在页面本身上,它显示以下错误:致命错误:无法将用户类型的对象用作数组"
我在做什么错了?
PHP:
$user = new User();
$email = $_SESSION['email'];
$user->getUserInfoByEmail($email);
var_dump($user['avatar']);
功能:
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();
}
这是因为您将$user
对象用作数组(因为错误消息使完全清晰可见).>
更改:
var_dump($user['avatar']);
对此:
var_dump($user->avatar);
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();
}
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);
这篇关于致命错误:不能将用户类型的对象用作数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!