pakai fungsi fputcsv, misal:
<?php
$csv = fopen('test.csv','w');
$sql = mysql_query("select * from nama_table");
fputcsv($csv, array('Kolom1','Kolom2', 'Kolom3');
while($data = mysql_fetch_array($sql)){
fputcsv($csv, array($data['kolom1'], $data['kolom2'], $data['kolom3']));
}
fclose($csv);