Daniel Le
Daniel Le

Reputation: 199

sql group by is duplicating results

I have trying to create a list of manufactures with a count.

This is my query

$query = $this->pdo->prepare('SELECT manufacture, count(*) AS count
                                      FROM listed_watches
                                      GROUP BY manufacture');

But when i do print_r, its duplicating the results. It is showing "manufacture" and "count" but its also showing [0] and [1], how come?

I just want it to show [manufacture], [count]

Array
(
    [0] => Array
        (
            [manufacture] => Audemars Piguet
            [0] => Audemars Piguet
            [count] => 2
            [1] => 2
        )

    [1] => Array
        (
            [manufacture] => Bell and Ross
            [0] => Bell and Ross
            [count] => 3
            [1] => 3
        )

    [2] => Array
        (
            [manufacture] => Bulova
            [0] => Bulova
            [count] => 1
            [1] => 1
        )

)

Upvotes: 0

Views: 47

Answers (1)

Spoke44
Spoke44

Reputation: 988

try this :

$result = $query->fetchAll(PDO::FETCH_ASSOC);

You can use by default this fetching method in the initialization of your connection with :

$pdo_options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;

Upvotes: 1

Related Questions