eug*_* a. 6 php mysql ajax large-data export-to-csv
我正在开发一个项目,我需要从包含近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);
Run Code Online (Sandbox Code Playgroud)
正如此评论中所解释的:/sf/answers/842886901/使用 mysqldump 可能是最好的选择。如果需要,您甚至可以通过 php 使用 exec() 命令执行此操作,如下所述:php exec() - mysqldump 创建一个空文件
| 归档时间: |
|
| 查看次数: |
8481 次 |
| 最近记录: |