PHP PDO: Call to a member function fetch() on bool

2019-08-20 09:44发布

So I'm currently trying to do a PDO SELECT Request, but when executing and fetching the extracted data, this error shows up:

1 - Fatal error: Uncaught Error: Call to a member function fetch() on boolean in C:\wamp64\www\NewKali\includes\user.inc.php on line 53    
2 - Error: Call to a member function fetch() on boolean in C:\wamp64\www\NewKali\includes\user.inc.php on line 53

This is where I call the function:

include 'includes/user.inc.php';
$userOBJ = new User;
if($userOBJ->isAdmin($_SESSION['session_u-name'])){
    AdminControl();
}

Code:

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

    $stmt = $this->Connect()->prepare("SELECT * 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 ($query1->fetch() as $row) {
             if($row['admin_db'] == 1){
                return true;
             } else {
                return false;
             }
         }
    }
}

The first error says that I'm not handling the PDO errors, which I think that I'm already handling any PDO error in my code, but somehow still gets detected as I'm not doing so... (Correct me if wrong)

Second error states that calling PDO->fetch() is returning a boolean, but I'm requesting data, so it's not able to continue with the following code... I don't get why this is showing... The "username_db" var in the query is the same as the one that I have in my DB.

In the same file as the function above, I have this next function and when called, it does fine

public function RegisterUser($user, $pwd, $mail){
    $u_Insert = $user;
    $p_Insert = $pwd;
    $m_Insert = $mail;

    $stmt = $this->Connect()->prepare("INSERT INTO user_secure(username_db, password_db) VALUES (?,?)");
    $query1 = $stmt->execute([$u_Insert, $p_Insert]);

    $stmt = $this->Connect()->prepare("INSERT INTO user_info(mail_db) VALUES (?)");
    $query2 = $stmt->execute([$m_Insert]);

    if($query2 && $query1){
        return true;
    } else {
        return false;
    }
}

Is there something that I'm missing? I have already checked this thread but I'm still in the exact position...

Thank you for your time

(I'm still learning PDO, sorry if my code isn't clean)

1条回答
聊天终结者
2楼-- · 2019-08-20 10:33

This line here is one of the reasons. execute returns true or false indicating if the query succeeded or failed.

$query1 = $stmt->execute([$userToGet]);

In a sense, $query1 is a boolean. Now in these lines, you are trying to access the fetch method from $query1 which is a boolean.

  foreach ($query1->fetch() as $row) {
         if($row['admin_db'] == 1){
            return true;
         } else {
            return false;
         }
     }

To get the row, you need to write it like this:

$results = $stmt->fetch();

or in your case:

  foreach ( $stmt->fetch() as $row) {
         if($row['admin_db'] == 1){
            return true;
         } else {
            return false;
         }
     }
查看更多
登录 后发表回答