Cannot use object of type PDOStatement as array

后端 未结 2 1174
日久生厌
日久生厌 2021-01-22 05:49

I want to check if some column of specify user is holding a value higher than 0.

Problem

When doing the query, and then executing it

2条回答
  •  甜味超标
    2021-01-22 06:19

    From:

    $admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = :username");
    [...]
    if ($settings['create_admins'] > 0 || $admin['super_admin'] > 0 ) {
    

    $admin is of type PDOStatament which is a class and not an array. Therefore you can't call the [] operator on it.

    Also you should really not alway assign $admin to the return result of every method because most of the PDOStatament's methods return boolean values:

    $admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = :username");
    $admin->bindValue(':username', $_SESSION['user']);
    $admin->execute();
    

    To retrieve the super_admin column from the admin table you should add (after the execute() statement):

    $result = $admin->fetch(PDO::FETCH_ASSOC);
    

    which will populate (hopefully, it depends on what's the table schema) $result['super_admin'].

提交回复
热议问题