使用PDO连接数据库,执行查询并在一行中获取结果

时间:2013-06-29 22:22:35

标签: php pdo

此代码工作正常:

$query = "SELECT * FROM user WHERE id = 1;";
$stmt = $this->openDb()->prepare($query);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

为什么此代码不起作用并向我提供错误Fatal error: Call to a member function fetchAll() on a non-object in

$result_set = $this->openDb()->prepare("SELECT * FROM user WHERE id = 1;")->execute()->fetchAll(PDO::FETCH_ASSOC);

1 个答案:

答案 0 :(得分:1)

$ stmt-> execute();不返回对象,它返回一个布尔值。 http://www.php.net/manual/de/mysqli-stmt.execute.php