我正在尝试将MYSQL查询的结果写入CSV并出现问题。查询工作正常,当我在powershell中运行时,它会在powershell中打印完美的CSV数据,但是在我的CPU上找不到这个文件。
我怎样才能给CSV写信呢?
$result = $conn2->query(
"SELECT firstn
, lastn
, extension
, Recieved
, RecievedKnown
, Outbound
, outboundKnown
, Missed
, MissedKnown
, CallingNumber
, CalledNumber
, starttime
, endtime
, duration
, HOLDTIMESECS
, TERMINATIONREASONCODE
FROM (
SELECT u.firstn
, u.lastn")
//Long query, removing unnecessary code
);
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
while ($fieldinfo = mysqli_fetch_field($result)) {
$headers[] = $fieldinfo->name;
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="dailyReportTestPHP.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}发布于 2017-10-03 20:34:31
您需要删除标题并编辑行以将路径添加到文件中:
$ fp = fopen ('php: // output', 'w');,例如
$ fp = fopen ('c:\\folder\\resource.txt', 'w');FOR HTTP:
if ($result) {
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=dailyReportTestPHP.csv");
header("Pragma: no-cache");
header("Expires: 0");
$fp = fopen('php://output', 'w');
fputcsv($fp, $headers);
while ($row = $result->fetch_row()) {
fputcsv($fp, $row);
}
fclose($fp);
die;
}https://stackoverflow.com/questions/46552915
复制相似问题