在布尔错误时调用成员函数execute()

问题描述 投票:0回答:3

这是我的代码:

    public function getTypeByID($typeId) {
    $stmt = $this->conn->prepare("SELECT type_id, description FROM type WHERE type_id = ? ");
    $stmt->bind_param("i", $typeId);
    if ($stmt->execute()) {
        $stmt->bind_result($id_type, $description);
        $stmt->fetch();
        $type = array();
        $type["type_id"] = $id_type;
        $type["description"] = $description;
        $stmt->close();
        return $type;
    } else {
        return NULL;
    }
}

我收到此错误:

致命错误:调用布尔值的成员函数execute()。

我尝试在 Mysql (PhpMyAdmin) 上执行查询,它有效。

我的类型表结构:

enter image description here

php mysql
3个回答
1
投票

您的 sql 确实命名了该表,但我只是遇到了一个相同错误的实例,将我带到了此页面。

在 SQL 中调用“数据库名称”或不存在的表而不是“表名称”也会产生此错误。


0
投票

可能是因为type是MySQL中的保留字。 请参阅:https://dev.mysql.com/doc/refman/5.5/en/keywords.html 尝试将表名更改为其他。


0
投票

type
是MySQL的保留字。
要解决此问题,您需要在语句中添加单引号。

示例

SELECT 'type_id', 'description' FROM 'type' WHERE 'type_id' .........
© www.soinside.com 2019 - 2024. All rights reserved.