我想下載一個MySQL表作爲CSV文件,但我得到這個錯誤「允許的內存大小1342177280字節用盡」。我想這是因爲該文件是第一次創建,然後下載。我怎樣才能確定用戶可以從0開始下載文件,在這種情況下,我認爲我不需要太多內存。PHP:下載MySQL表
這是我到目前爲止的代碼:
$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;
您的代碼的難看解決方案是ini_set('memory_limit', - 1);它不會給這個錯誤。 –
在SO上搜索'MYSQL SELECT INTO OUTFILE'並給我一杯飲料,因爲使用它之後你真的會很開心,相比於你自己的方法:) –
另一個解決方案是你在服務器上寫完整個文件並允許使用它。 –