MySQL PDO-设置默认提取模式? [英] MySQL PDO - Setting Default Fetch Mode?

查看:101
本文介绍了MySQL PDO-设置默认提取模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以今天,我终于要从标准的PHP MySQL函数过渡到PDO.我注意到在将数据作为对象获取时,必须运行类似于以下内容的行:

So today, I'm finally making the transition from standard PHP MySQL functions to PDO. I noticed when fetching data as an object, we must run a line similar to the following:

$STH = $DBH->query('SELECT name, addr, city from folks');
$STH->setFetchMode(PDO::FETCH_OBJ);

$result = $STH->fetch();

我的问题是关于第2行的.是否有一种方法可以将其设置为默认行为,以便我们不必每次都希望运行查询时都设置获取模式?这对我来说似乎很烦.当然没有必要这样做吗?

My question is regarding line 2. Is there a way to set this as the default behavior so that we don't need to set the fetch mode every single time we wish to run a query? This seems pretty annoying to me. Surely it's not necessary to do this?

推荐答案

您可以为PDO对象设置默认的获取模式:

You can set the default fetch mode for the PDO object:

$DBH->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

当然,这是在初始化$ DBH(PDO)对象后立即执行的.

This, of course, you do as soon as you've initialized your $DBH (PDO) object.

(有关详细文档,请参见 http://www.php. net/manual/de/pdo.setattribute.php )

(For detailed documentation on this, see http://www.php.net/manual/de/pdo.setattribute.php)

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

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