Lelio Faieta Lelio Faieta - 1 year ago 73
PHP Question

perform two different fetch on the same query result

I have this query:

$all = $dbh->query("SELECT DISTINCT movimenti.nome, SUM(movimenti_carta.importo)
FROM movimenti_carta JOIN movimenti ON movimenti_carta.movimento=movimenti.id
WHERE month(data)='$mese' AND year(data)='$anno' GROUP BY movimenti.nome");

It retrieves two columns from my db. What I want to do is to put each column in a separate array.
If I do:


I get the values for the first column with the format I am looking for.
I tried to do:


after the first fetch but
is unset by the first fetch and the result is that
is an empty array (I was pretty sure of this but did give it a try).
I can build the arrays in php after I fetch an array with both columns but I was wondering how to get my goal using PDO api.

Answer Source

Of course it will never work this way, as fetchAll() returns data only once.

The only idea I could think of is PDO::FETCH_KEY_PAIR constant:

$data = $all->fetchAll(PDO::FETCH_KEY_PAIR);
$labels = array_keys($data);
$values = array_values($data);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download