致命错误:在{path}中调用boolean上的成员函数fetch()

时间:2016-06-16 14:22:18

标签: php mysql sql pdo

这是我的代码:

$money = $db_con
->prepare(" SELECT SUM(asker_amount) asker, SUM(responder_amount) responder
            FROM money WHERE post_id = ? AND author_id = ? AND paid IS NULL")
->execute(array($ques_id, $author_ques_id))
->fetch(PDO::FETCH_ASSOC);
$asker_amount = $money['asker'];
$responder_amount = $money['responder'];

它不会执行并抛出此错误:

  

致命错误:在第5行->fetch( ...){file path}的布尔值上调用成员函数fetch()

好的,怎么了?我该如何解决?

注意:该查询也可以,我已经在phpmyadmin中对其进行了测试。

1 个答案:

答案 0 :(得分:3)

试试这个。

$query = $db_con->prepare(" SELECT SUM(asker_amount) asker, SUM(responder_amount) responder
        FROM money WHERE post_id = ? AND author_id = ? AND paid IS NULL");
$query->execute(array($ques_id, $author_ques_id));
$money = $query->fetch(PDO::FETCH_ASSOC);
$asker_amount = $money['asker'];
$responder_amount = $money['responder'];

您应该仅为$money分配fetch的结果。