Just as another answer to how you could do this, here's what I usually do:
$csvrows = array(
array(
'column 1',
'column 2',
'column 3',
'column 4',
'column 5'
)
);
while($row = mysql_fetch_assoc($result))
{
$csvrows[] = array(
$row['column 1'],
$row['column 2'],
$row['column 3'],
$row['column 4'],
$row['column 5']
);
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=somefile.csv');
$fp = fopen('php://output', 'w');
foreach($csvrows as $row)
{
fputcsv($fp, $row);
}
fclose($fp);
If you wanted to change the delimiter to be a semi-colon, just add a parameter:
fputcsv($fp, $row, ';');
May as well use the built-in functions PHP has 😉