我正在尝试导出&使用以下代码下载包含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;
但只获得标题数组但没有记录
需要帮助来修复代码。
此致
答案 0 :(得分:1)
在循环中以相同的方式写出来怎么样?
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;
答案 1 :(得分:0)
将结果放入$ output stream
while($rows = mysql_fetch_array($result)) {
fputcsv($output, array($rows['ReferenceNo'],
date('Y-m-d',strtotime($rows['DepartDate'])).' '.$rows['DepartTime'],
$rows['DepartTerminal'],
$rows['DepartFlight']
)
);
}