PHP PDO grouping results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$stmt = $pdo_connection->query('SELECT id, name, surname FROM people'); | |
$people = $stmt->fetchAll(\PDO::FETCH_GROUP | \PDO::FETCH_UNIQUE | \PDO::FETCH_ASSOC); | |
/* | |
$people will now contain: | |
array( | |
1 => array( | |
'name' => 'Juan', | |
'surname' => 'Sorin', | |
), | |
... | |
) | |
if you omit \PDO::FETCH_UNIQUE, array of arrays will returned | |
(useful if multiple rows can have same id) | |
array( | |
1 => array( | |
0 => array( | |
'name' => 'Juan', | |
'surname' => 'Sorin', | |
), | |
1 => ... | |
), | |
... | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment