NotionCommotion Posted November 12, 2016 Share Posted November 12, 2016 I am looking at some old code I wrote, and I have no idea why I did it. What is the purpose of second argument to PDO::query()? Looking at http://php.net/manual/en/pdo.query.php, it doesn't even appear that PDO::FETCH_OBJ is valid. foreach($this->db->query('SELECT id, name FROM points',PDO::FETCH_OBJ) as $point) { // ... } Quote Link to comment https://forums.phpfreaks.com/topic/302523-purpose-of-second-argument-to-pdoquery/ Share on other sites More sharing options...
ginerjm Posted November 12, 2016 Share Posted November 12, 2016 (edited) When I looked up fetch, it shows clearly that FETCH_OBJ is a valid option. You have to look under pdo_statement: http://php.net/manual/en/pdostatement.fetch.php Edited November 12, 2016 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/302523-purpose-of-second-argument-to-pdoquery/#findComment-1539256 Share on other sites More sharing options...
kicken Posted November 12, 2016 Share Posted November 12, 2016 Note: Although this function is only documented as having a single parameter, you may pass additional arguments to this function. They will be treated as though you called PDOStatement::setFetchMode() on the resultant statement object. Basically it just lets you prime the fetch style of the resulting statement object. I'd say it's really not that useful as generally you'd just use the same fetch style throughout the application and that should be set on the original PDO object using PDO::setAttribute with PDO::ATTR_DEFAULT_FETCH_MODE. If for some reason I want to use an alternate fetch style, I prefer to just call PDOStatement::fetch directly with the appropriate style. 1 Quote Link to comment https://forums.phpfreaks.com/topic/302523-purpose-of-second-argument-to-pdoquery/#findComment-1539258 Share on other sites More sharing options...
NotionCommotion Posted November 12, 2016 Author Share Posted November 12, 2016 (edited) Thanks ginerjm, Yes, I am with you regarding the fetch style when used with the fetch method, but wasn't sure when it came to the query method. Thanks kicken, I haven't ever depended upon PDO::setAttribute with PDO::ATTR_DEFAULT_FETCH_MODE, however, I always specify the fetch style with my fetch() or fetchAll() (but not fetchColumn) methods, and I guess that is why I didn't recognize it. Edited November 12, 2016 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/302523-purpose-of-second-argument-to-pdoquery/#findComment-1539259 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.