我遇到了和你一样的问题。就我而言,解决方案是我们只需要使用
include 'connection_file.php';
after
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
意味着你的头文件必须是文件的第一条语句。
导出 csv 的完整 PHP 代码是:
<?php
if(isset($_POST['Export'])){
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
include '../dbcon.php';
$output = fopen("php://output", "w");
fputcsv($output, array('id', 'name', 'phone', 'Employee Name', 'district', 'taluka', 'address'));
$query = "SELECT * from export_buffer ORDER BY id DESC";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($output, $row);
}
fclose($output);
}
?>