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...
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 ...
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.
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.
$connection = new PDO($connection_string); $connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With