Jump to content

Purpose of second argument to PDO::query()?


NotionCommotion

Recommended Posts

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) {
   // ...
}
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 by NotionCommotion
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.