0

Recently I had a problem with one of my PHP codes, and made this thread and I got a solution on that, but when applying that solution, another error came up!

Warning: Illegal string offset 'admin_db' in C:\wamp64\www\NewKali\includes\user.inc.php on line 55

This error is complaining about this section of my code:

public function isAdmin($user){
    $userToGet = $user;

    $stmt = $this->Connect()->prepare("SELECT admin_db FROM user_secure WHERE username_db=?");
    $query1 = $stmt->execute([$userToGet]);

    if(!$query1)
    {
      die("Execute query error, because: ". print_r($this->Connect()->errorInfo(),true) );
    }else{
        foreach ( $stmt->fetch() as $row) {
            $value = $row["admin_db"];
                 if($value == 1){
                    return true;
                 } else {
                    return false;
                 }
             }
    }
}

It happens here:

$value = $row["admin_db"];

Even when this error happens, the function is still returning a boolean value, which I require in another code.

I'm still new to PHP, and I don't know what this error means, or what am I doing wrong...

Thank you for your time!

Alan Maldonado
  • 123
  • 1
  • 13

1 Answers1

1

try use PDO::FETCH_ASSOC

fetchAll(PDO::FETCH_ASSOC)

I think your fetch is not returning an array indexed by column names but returns an array indexed by 0-indexed column number.

http://php.net/manual/en/pdostatement.fetch.php

George Myl
  • 301
  • 2
  • 5