CSV е тривиален с fputcsv
:
$fh = fopen('output.csv', 'w') or die("can't open file");
// output header and first row
$row = mysql_fetch_assoc($result)
fputcsv($fh, array_keys($row));
fputcsv($fh, $row);
// output the remaining rows
while ($row = mysql_fetch_assoc($result)) {
fputcsv($fh, $row);
}
fclose($fh);