PDO fetchAll group key-value pairs into assoc array

For your problem there is pretty ready solution, that is:

$q = $db->query("SELECT `name`, `value` FROM `settings`;");
$r  = $q->fetchAll(PDO::FETCH_KEY_PAIR);

Works for me, on PostgreSQL 9.1, and PHP 5.3.8 running on Windows 7 x64.

Leave a Comment