使用PHP将MySQL列导出为CSV

时间:2014-05-03 06:45:59

标签: php mysql csv

我正在尝试使用PHP将MySQL表的某些列导出到csv文件。到目前为止,我已经提出了以下代码,它会在" tabella"上打印出数据。文件在/ localhost中。 问题是它返回一个空的csv文件。我不明白为什么。有什么想法吗?谢谢!

<?php
// create a file pointer connected to the output stream
$output = fopen('tabella', 'w');
// output the column headings
fputcsv($output, array('giorno', 'mese', 'anno', 'descrizione', 'durata'));
// fetch the data
mysql_connect('localhost', 'root', '');
mysql_select_db('ore');
$rows = mysql_query('SELECT giorno,mese,anno,descrizione,((((ora_fine*60)+(min_fine))-((ora_ini*60)+(min_ini)))/60) FROM consuntivo');
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)){
fputcsv($output, $row);
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=tabella.csv');
fclose($output);
?>

1 个答案:

答案 0 :(得分:0)

将文件名更改为tabella.csv

$output = fopen('tabella.csv', 'w');