Cannot use object of type PDOStatement as array
mysql, pdo, php
Solution
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']`.
Problem
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, Im getting this error: ``` Fatal error: Cannot use object of type PDOStatement as array in C:\xampp\htdocs\recover\admin\create.php on line 40 ``` My code (The query + execute): ``` if (isset($_SESSION['user'])) { $admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = :username"); $admin->bindValue(':username', $_SESSION['user']); $admin->execute(); ``` Line of error (40): ``` if ($settings['create_admins'] > 0 || $admin['super_admin'] > 0 ) { ``` Question: Why am I getting this error? How do I fix it? I tried doing this: ``` $admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = :username"); $admin = $admin->bindValue(':username', $_SESSION['user']); $admin = $admin->execute(); ``` and getting another error: ``` Fatal error: Call to a member function execute() on a non-object in C:\xampp\htdocs\recover\admin\create.php on line 38 ``` Thanks! EDIT: I need the ->fetch object, but I have just done this, and got rid of the errors.. But it doesn't affect? I mean I am echoing that row, and it gives me a null (nothing). Why? ``` $admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = ".$_SESSION['user'].""); $admin = $admin->fetch(); ```