douqian9729 2018-11-16 02:38
浏览 86
已采纳

PHP PDO:在布尔值上调用成员函数fetch()

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条回答 默认 最新

  • dqjjw04440 2018-11-16 02:49
    关注

    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;
             }
         }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备