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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵