我希望下载一个MySQL表格作为CSV文件,但是我遇到了这个错误:“内存大小已耗尽1342177280字节”。我猜这是因为文件首先被创建,然后再下载。在这种情况下,我该如何实现用户可以从0开始下载文件,我认为这样我就不需要那么多内存。
这是目前我的代码:
这是目前我的代码:
$output = "";
$table = "export_table";
$sql = mysql_query("select * from $table");
$columns_total = mysql_num_fields($sql);
// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
// Download the file
$filename = "export.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;