Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to set a default PDO fetch mode?

Tags:

php

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....

Thanks!

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...

like image 357
Matt Avatar asked Oct 08 '10 19:10

Matt


People also ask

How does PDO fetch work?

fetch does not store information in memory and it works on row to row basis, so it would go through the result set and return row 1, than again would go to the result set and than again return row 2 mind here that it will not return row 1 as well as 2 but would only return row 2, so fetch will not store anything into ...

What does FETCH do in php?

Introduction to the PHP fetch() method The fetch() method allows you to fetch a row from a result set associated with a PDOStatement object. Internally, the fetch() method fetches a single row from a result set and moves the internal pointer to the next row in the result set.

What is PDO :: Fetch_assoc?

PDO::FETCH_ASSOC. Returns an array indexed by column name as returned in your result set. PDO::FETCH_BOTH (default) Returns an array indexed by both column name and 0-indexed column number as returned in your result set.


1 Answers

$connection = new PDO($connection_string); $connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); 
like image 194
Anon Avatar answered Oct 09 '22 09:10

Anon