我正在尝试导出&使用以下代码下载包含mysql记录的CSV文件;
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=DepReport_'.$from.'_'.$to.'.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Reference',
'Depart Date / Time',
'Depart Terminal',
'Depart Flight'
)
);
$i = 2;
while($rows = mysql_fetch_array($result)) {
fputcsv($data[$i], array($rows['ReferenceNo'],
date('Y-m-d',strtotime($rows['DepartDate'])).' '.$rows['DepartTime'],
$rows['DepartTerminal'],
$rows['DepartFlight']
)
);
$i++;
}
fputcsv($output, $data);
exit;
但只有标题数组,但没有记录
需要帮助修复代码。
对
在循环中以同样的方式写出来怎么样?
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=DepReport_'.$from.'_'.$to.'.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Reference',
'Depart Date / Time',
'Depart Terminal',
'Depart Flight'
)
);
$i = 2;
while($rows = mysql_fetch_array($result)) {
//change to $output so it writes lines to same handler
fputcsv($output, array($rows['ReferenceNo'],
date('Y-m-d',strtotime($rows['DepartDate'])).' '.$rows['DepartTime'],
$rows['DepartTerminal'],
$rows['DepartFlight']
)
);
$i++;
}
//fputcsv($output, $data);
//don't need anymore
exit;
将结果放入$output流
while($rows = mysql_fetch_array($result)) {
fputcsv($output, array($rows['ReferenceNo'],
date('Y-m-d',strtotime($rows['DepartDate'])).' '.$rows['DepartTime'],
$rows['DepartTerminal'],
$rows['DepartFlight']
)
);
}