Re: Unexpected values in an associative array

From: Date: Wed, 01 Aug 2007 07:01:08 +0000
Subject: Re: Unexpected values in an associative array
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-259971@lists.php.net to get a copy of this message
On 7/31/07, Ken Tozier <kentozier@comcast.net> wrote: > > ... > // set fetch prefs > $this->db->setAttribute(PDO:: FETCH_ASSOC, > true); // also tried 1 > ... > Is that the way to do it? Hmm.. Maybe I sent you in the wrong direction - I can't find any docs on using setAttribute to set the fetch mode. Anyway, setAttribute always works like this: setAttribute(attribute_to_set,value_to_set_to); I was expecting something like: setAttribute(PDO::FETCH_MODE, PDO::FETCH_ASSOC); I don't think that works though, can't find anything in docs relating to attribute called FETCH_MODE. Anyway I dug in some code and this is what you can do: foreach ($pdo->query($query, PDO::FETCH_ASSOC)) {} Query can take a second parameter. Another option: PDOStatement->setFetchMode(PDO::FETCH_ASSOC).. so your original code goes from: foreach ($pdo->query($query) as $row) {} To: $statement = $pdo->query($query); $statement->setFetchMode(PDO::FETCH_ASSOC); foreach( $statement as $row) {} There must be a way to set it with setAttribute for the connection though, instead of just on a per-statement basis... Travis Doherty

« previous php.general (#259971) next »