是否可以设置默认的 PDO 获取模式?

2021-12-26 00:00:00 php pdo

在我检索数据之前,我总是必须输入:

Before I retrieve data I always have to type:

$STH->setFetchMode(PDO::FETCH_OBJ);

为了使我的代码更具可读性,如果我可以在某处设置默认模式会很棒....

In the interest of making my code more readable it would be great if I could set a default mode somewhere....

谢谢!

编辑.我最初希望我可以将 PDO:FETCH_OBJ 添加到我运行的 setAttribute 代码中我连接到数据库,但这似乎不起作用......

Edit. I was originally hoping I could add PDO:FETCH_OBJ to the setAttribute code I run when I connect to the DB, but that doesn't seem to work...

推荐答案

$connection = new PDO($connection_string);
$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

相关文章