PHP PDO:在布尔值上调用成员函数fetch() [英] PHP PDO: Call to a member function fetch() on boolean

查看:152
本文介绍了PHP PDO:在布尔值上调用成员函数fetch()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我当前正在尝试执行PDO SELECT请求,但是在执行和提取提取的数据时,会显示此错误:

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;
             }
         }
    }
}

第一个错误表明我没有处理PDO错误,我认为我已经在代码中处理了任何PDO错误,但是由于我没有这样做,仍然以某种方式被检测到...(正确如果我错了我

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)

第二个错误指​​出调用PDO-> fetch()返回一个布尔值,但是我正在请求数据,因此无法继续以下代码... 我不明白为什么会这样显示...查询中的"username_db"变量与数据库中的变量相同.

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.

在与上述函数相同的文件中,我具有下一个函数,当调用该函数时,效果很好

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

(我仍在学习PDO,如果我的代码不干净,对不起)

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

推荐答案

此处的这一行是原因之一. execute 返回true或false,指示查询是成功还是失败.

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

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

从某种意义上说,$ query1是一个布尔值. 现在,在这些行中,您尝试从$ query1访问获取方法,该方法是一个布尔值.

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();

或您的情况:

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

这篇关于PHP PDO:在布尔值上调用成员函数fetch()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆