小编McH*_*bie的帖子

在PHP中,如何设置默认的PDO Fetch Class?

使用PDO ::的setAttribute,怎么设置我什么时候提供的类名PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_CLASS.

这是我正在使用的代码..我想设置它所以我的所有行都作为以下实例返回DB_Row:

class DB_Row extends ArrayObject {}

$db = new PDO('mysql:dbname=example;host=localhost', 'user', 'pass');
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);

$stmt = $db->query("SELECT * FROM `table` WHERE `id` = 1;");

$row = $stmt->fetch(); // I want a DB_Row by default!
Run Code Online (Sandbox Code Playgroud)

上面的代码导致了一个,PDOException因为没有分配DB_Row类名.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General    error: No fetch class specified
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

提前致谢..

解决方案:我使用了fireeyedboy的回答.它对我的情况起了最好的作用,因为我已经将PDOStatement扩展用于记录目的......

class DB extends PDO {
    public function __construct($host = null, $user = null, …
Run Code Online (Sandbox Code Playgroud)

php pdo

6
推荐指数
2
解决办法
6320
查看次数

标签 统计

pdo ×1

php ×1