5

Possible Duplicate:
Is is possible to set a default PDO fetch mode?

I have searched the internet and this site and could not find an solution.

Is there a way to apply $this->query->setFetchMode(PDO::FETCH_ASSOC); globally for all PDO queries? I find myself repeating the line over and over again.

I have tried applying it to the initial PDO connection: $this->connection->setFetchMode() but that throws an exception.

Community
  • 1
  • 1
Phil
  • 10,948
  • 17
  • 69
  • 101
  • Consider the disadvantages of that, your code could be less portable because of the global setting dependency. – MrCode Dec 12 '12 at 15:49
  • 1
    @MrCode - It's "global" to the object / connection he is creating, it's not global for all PDO objects created in the current session. I see no portability issues here. – nickb Dec 12 '12 at 15:52
  • 1
    Ah, nickb, I didn't couldn't find that! Thank you! – Phil Dec 12 '12 at 15:52
  • @nickb I realise the scope. The point I was making is if you are dependency injecting the PDO object into a bunch of code (as should be done), or have it as a singleton, said code won't function if you move it to another environment where the PDO object doesn't have the setting. – MrCode Dec 12 '12 at 16:15

1 Answers1

16

Since 5.2

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • 2
    You can do this when you first instansiate it as well new PDO('mysql:host='.DBHOST.';dbname='.DBNAME,DBUSER,DBPASS, array(PDO::ATTR_PERSISTENT => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC)); – relipse Apr 22 '13 at 01:53