473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Php pdo::fetch_*

guillermobytes
Hi,

I would like to set the fetch mode for all the pdoStatements how can i do that.
For the moment i have only found a method that will set the fetch mode for the pdoStatement instance the method is applied to.

ex:
Expand|Select|Wrap|Line Numbers
  1. $connection = new PDO(...);
  2. $sql = "SELECT * FROM table";
  3. $statement = $connection->prepare($sql);
  4. $statement->setFetchMode(PDO::FETCH_ASSOC);
  5. $statement->execute();
  6. $rows = $statement->fetchAll();
this works fine for that instance of pdoStatement. but i would like it to be permanent.

so is there any static pdoStatement::setFetchMode(); kind of method
or PDO instance method : $pdo->setFetchMode();

thanks
Mar 28 '10 #1
4 2629
Dormilich
8,658 Expert Mod 8TB
@guillermobytes
there is no static setFetchMode() method (why should there be any, every statement object could have a different fetch mode). from the OOP point-of-view, it does not make sense to have a static fetch mode.

the PDO class does only deal with DB connection, data retrieval is done by PDOStatement, but you can write your own implementation (e.g. a wrapper or an extended class)
Mar 29 '10 #2
lol Dormilich, in almost every answer I get from you there is this sentence :
from the OOP point-of-view, it does not make sense
i may have completely missed the OOP concepts or maybe you are not enough open minded. probably it's the first option.. but consider this, and then tell me if I missed something:
there is by default a fetch mode wich is FETCH_BOTH (I think)(it's FETCH_ASSOC and FETCH_NUM).
the point is that the default fetch mode does not satisfy my needs, because i use 99% of the time the FETCH_ASSOC, there for i would like to change the default value to fit my needs.
If i have to change the fetch mode to FETCH_ASSOC every time i create a pdoStatement (with a wrapper) then it makes n changes (given n is the number of pdoStatement instances created).
However if i change it once from the bootstraper and then ocasionally when i need the other fetch mode (FETCH_NUM) i change it from the pdoStatement instance, i would only get 1n/100 which is a good improvement.

so whenever there is a default value that gets replicated over every instance, with the right to change the instance value, i think it would make sense to make that default property static, to allow the programmer to change that property if desired like this :
Expand|Select|Wrap|Line Numbers
  1. class pdoStatement
  2. {
  3.     private static $_defaultFetchMode = pdo::FETCH_BOTH;
  4.  
  5.     private $_fetchMode;
  6.  
  7.     public function __construct()
  8.     {
  9.          //set the instance fetchMode from default fetch mode
  10.          $this->setFetchMode(self::$_defaultFetchMode);
  11.     }
  12.  
  13.     public function setFetchMode($fetchMode)
  14.     {
  15.         //ensure fetch mode is supported
  16.         self::isSupportedFetchMode($fetchMode);
  17.         //fetch mode supported
  18.         $this->_fetchMode = $fetchMode;  
  19.     }
  20.  
  21.     static public function setDefaultFetchMode($fetchMode)
  22.     {
  23.         //ensure fetch mode is supported
  24.         self::isSupportedFetchMode($fetchMode);
  25.         //fetch mode supported
  26.         self::$_defaultFetchMode = $fetchMode;  
  27.     }
  28.  
  29.     static public function isSupportedFetchMode($fetchMode)
  30.     {
  31.         switch ($fetchMode) {
  32.             case FETCH_BOTH;
  33.             break;
  34.             case FETCH_ASSOC;
  35.             break;
  36.             case FETCH_NUM;
  37.             break;
  38.             case etc.
  39.             break
  40.             default;
  41.                 throw new pdoStatement_Exception('Error : fetch mode not supported');
  42.             break;
  43.         }
  44.     }
  45. }
it saves some resources and time to write a wrapper.


we could even write the second option i gave, to set the fetchMode from the pdo class. That seems to make sense too because the pdo class creates pdoStatments, so it could call pdoStatement::setFetchMode() before returning the pdoStatement. (i wont code it, you get what i mean)

anyway I appreciate your answer and if you see any mistake in my point, i would be pleased if you made some remarks.
Mar 29 '10 #3
Markus
6,050 Expert 4TB
This is a feature that could obviously be useful. I'll write a patch and submit it to the PDO guys and see what they say.
Mar 29 '10 #4
Markus that'd be great! keep me up to date, if it is aproved/rejected.
Mar 29 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rowan | last post by:
For some reason when I do a fetchall on a PDO query the array returned has duplicated keys. see below. SELECT name_first, name_last, personal_street, personal_phone_home, personal_phone_cell...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.