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
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']
.
try this:
$sql = "SELECT * FROM admin WHERE username = ?";
$stmt = $CONNECT_TO_DATABASE->prepare($sql);
$stmt->execute(array($_SESSION['user']));
$admin = $stmt->fetch();
if($admin) {
//do something if query returns row(s)
}