【问题标题】:Fatal error: Call to a member function fetchAll() on boolean [duplicate]致命错误:在布尔值上调用成员函数 fetchAll() [重复]
【发布时间】:2017-06-07 07:13:52
【问题描述】:
$selected_id = -1;
$selected_name = "";
if(isset($_GET['cid'])){
    $cid = $_GET['cid'];
    $res = $pdo->prepare("SELECT * FROM k WHERE id = :cid");
    $res->bindParam(":cid",$cid);
    $result = $res->execute();

    $rw = $result->fetchAll([$cid]);
    if($rw){
        $selected_id = $rw['id'];
        $selected_name = $rw['ime'];
    }
}

我只想选择 ID 并与已选择的 ID 进行比较,但总是出现此错误,任何人都可以帮助我,请...

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    $res->execute 返回一个布尔值,您将其保存在$result 变量中:

    http://php.net/manual/en/pdostatement.execute.php

    您使用的不是同一个对象,因此您不能使用该对象的fetchAll 方法。你应该使用相同的对象,所以你应该这样做:

    $selected_id = -1;
    $selected_name = "";
    if(isset($_GET['cid'])){
        $cid = $_GET['cid'];
        $res = $pdo->prepare("SELECT * FROM k WHERE id = :cid");
        $res->bindParam(":cid",$cid);
        $res->execute(); // Run the method
    
        $rw = $res->fetchAll([$cid]); // Store the fetchAll result in a new variable
        if($rw){
            $selected_id = $rw['id'];
            $selected_name = $rw['ime'];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 2018-05-06
      • 2015-02-08
      • 2016-09-24
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多