我有一个脚本可以将一个SQL查询输出到一个CSV文件,该文件可以正常工作,但只要导出的内容只包含一个':'文本,直到显示':'。
<?php
include 'connect.php';
date_default_timezone_set('Europe/London');
$date = date('Y-m-d');
//echo $date;
if(isset($_POST['export_query'])){
$querytemp=$_POST['export_query'];
}
//$querytemp = "SELECT * FROM poc_report_live WHERE Date = '$date'";
$result = mysql_query($querytemp);
//if (!$result) die($querytemp);
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) {
$headers[] = mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('charset = UTF-8');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_array($result, MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
?>