This article brings you relevant knowledge about PHP csv. It mainly introduces how to write millions of data into csv through script files in PHP. Let’s take a look at how to achieve it. I hope it will be helpful to everyone. helpful.
Requirements:
Write millions of data to csv.
Framework:
N The old framework many years ago cannot be found on Baidu, so the writing method is closer to the original
Analysis:
The amount of data is too large and cannot be written to csv download using the browser request, so write a script to retrieve it
Pseudo code:
//xxx - 根据自己项目替换 //调取脚本文件 exec(xxx); //脚本文件 //设置执行时间和内存 set_time_limit(0); ini_set('memory_limit', '128M'); //循环获取 $id = 0; $data = 'xxx'.'\n';//表头 while(true){ //SQL $list = xxx WHERE id > $id ORDER BY id ASC LIMIT 10000; //每次取1w防止数据库压力大,根据sql来,我这个有联表, if(empty($list)){ break; } foreach($list as $row){ $data .= '"' . $row['xxx'] . '",'; $data .= "\n"; $id = $row['id'];//更新最大id } //追加写入csv file_put_contents('xxx', mb_convert_encoding($data, 'gbk'),FILE_APPEND); unset($data);//基础不好不确定初始化能不能清内存就先unset了 $data = '';//初始化 }
Essentially, it is written in batches. When I first started experimenting, I planned not to write file_put_contents in the loop. Later, I found that when the amount of data is large, hundreds of thousands of data, $data memory will be exceeded. In addition, if there are more than one million data, it will be necessary. When exporting files, Excel does not support opening more than 1,048,576 rows. Theoretically, you can add the $all_count parameter to count the total number of current queries. If it exceeds one million, add new files.
If there is a better method or code optimization, please discuss it.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Let's talk about how to write millions of data into csv in php. For more information, please follow other related articles on the PHP Chinese website!

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
