Say I have a table
id,f1,f2
10,'aaa','aaaaaa'
20,'bbb','bbbbb'
Is there a way using PDO to query the DB and get the results as array:
[10=>['aaa','aaaaa'], 20=>['bbb','bbbbb'] ]
Yes, it's possible.
That's one of things that make PDO so great.
All you need is PDO::FETCH_UNIQUE constant:
$data = $pdo->query('SELECT id,f1,f2 FROM t')->fetchAll(PDO::FETCH_UNIQUE);
will give you desired result.
You may wish to browse other fetch modes for PDO in the article I wrote recently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With