Re: Unexpected values in an associative array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 7/31/07, Ken Tozier <kentozier@xxxxxxxxxxx> 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux