是否可以设置默认的 PDO 获取模式? [英] Is is possible to set a default PDO fetch mode?

查看:25
本文介绍了是否可以设置默认的 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);

这篇关于是否可以设置默认的 PDO 获取模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆