我正在做一个项目,需要从包含近10k行的数据库中提取数据,并将其导出为CSV。我尝试了常规方法来下载CSV,但我总是遇到内存限制问题,即使我们已经将memory_limit设置为256MB。
如果你们中有人遇到过相同的问题,请分享你们的想法和最佳解决方案或方法。
非常感谢大家的意见。
这是我的实际代码:
如果你们中有人遇到过相同的问题,请分享你们的想法和最佳解决方案或方法。
非常感谢大家的意见。
这是我的实际代码:
$filename = date('Ymd_His').'-export.csv';
//output the headers for the CSV file
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$filename}");
header("Expires: 0");
header("Pragma: public");
//open the file stream
$fh = @fopen( 'php://output', 'w' );
$headerDisplayed = false;
foreach ( $formatted_arr_data_from_query as $data ) {
// Add a header row if it hasn't been added yet -- using custom field keys from first array
if ( !$headerDisplayed ) {
fputcsv($fh, array_keys($ccsve_generate_value_arr));
$headerDisplayed = true;
}
// Put the data from the new multi-dimensional array into the stream
fputcsv($fh, $data);
}
// Close the file stream
fclose($fh);
mysqldump
的system
命令吗?这在过去解决了我所有的问题。 - rfoo