Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO MySQL fetchAll() using double the necessary memory in result?

Tags:

php

pdo

When I use ->fetchAll() with PDO, the resulting array looks like this when I do a print_r():

Array
(
    [0] => Array
        (
            [week] => 2006-03-05
            [0] => 2006-03-05
            [ath] => 112.89166667
            [1] => 112.89166667
        )

    [1] => Array
        (
            [week] => 2006-03-12
            [0] => 2006-03-12
            [ath] => 260.04527778
            [1] => 260.04527778
        )

    [2] => Array
        (
            [week] => 2006-03-19
            [0] => 2006-03-19
            [ath] => 219.23472222
            [1] => 219.23472222
        )

etc., etc.

Are the resulting values stored twice in memory? One under a numerical array index like 0 and 1, and the other under its named index, such as week or ath?

I am mainly just curious. I don't expect this to really impact my program significantly. Thanks.

like image 788
Brad Avatar asked Feb 26 '11 22:02

Brad


1 Answers

Are the resulting values stored twice in memory?

Yes. See the manual:

PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set

Use the optional $fetch_style parameter to change the way fetchAll() behaves.

$result = $sth->fetchAll(PDO::FETCH_ASSOC);
like image 173
Pekka Avatar answered Sep 28 '22 17:09

Pekka