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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序