MySQL PDO - Setting Default Fetch Mode?

随声附和 提交于 2020-01-13 03:55:09

问题


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();

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?


回答1:


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

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

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

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



来源:https://stackoverflow.com/questions/11037842/mysql-pdo-setting-default-fetch-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!