Dopo aver visto il tuo commento e che stai solo cercando di farne un CSV, puoi fare qualcosa del genere:
<?php
$query = $db->query('SELECT ItemID, GROUP_CONCAT(File SEPARATOR \'|$|\') AS Files FROM Table GROUP BY ItemID');
// Use a string that cant appear as part of the filename as the separator
$fh = fopen('items.csv', 'w');
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) {
$files = explode('|$|', $row['Files']);
fputcsv($fh, array_merge(array($row['ItemID']), $files));
}
fclose($fh);