当前位置:网站首页>PHP exports millions of data
PHP exports millions of data
2022-07-07 07:47:00 【php_ kevlin】
List of articles
Tips : because phpexcel and phpoffice Exporting data will overflow memory , So use php-Xlswriter# Preface
One 、 Install the extension
pecl install xlswriter
modify php.ini
extension = xlswriter.so
Then restart to make php-fpm Settings take effect
Two 、 Use steps
1. Import and stock in
The code is as follows ( Example ):
composer require viest/php-ext-xlswriter-ide-helper:dev-master
2. Read in the data
The code is as follows ( Example ):
<?php
require __DIR__ . '/vendor/autoload.php';
$dbms='mysql'; // Database type
$host='127.0.0.1'; // Database host name
$dbName='employees'; // Database used
$user='root'; // Database connection user name
$pass='123456'; // Corresponding password
$dsn="$dbms:host=$host;dbname=$dbName";
// By default, this is not a long connection , If you need a long database connection , You need to add one last parameter :array(PDO::ATTR_PERSISTENT => true) It's like this :
$dbh = new \PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
ini_set('memory_limit','1024M');
set_time_limit(0);
$config = [
'path' => './',
];
$excel = new \Vtiful\Kernel\Excel($config);
// Init File
$fileName = 'emp.xlsx';
// Fixed memory mode
$excel = $excel->constMemory($fileName, 'sheet11');
$excel = $excel->header([ 'id', 'salary', 'first_name', 'last_name']);
for($i=5000;$i<=1000000;$i+=5000){
$start = $i-5000;
$limit = 5000;
$sql = "SELECT * FROM salaries order by emp_no asc limit {
$start},{
$limit}";
//$sql = 'SELECT * FROM employees order by emp_no asc limit 10';
//var_dump($sql);die();
//$sql = "SELECT * FROM salaries limit 10";
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute();
$emps = $sth->fetchAll(PDO::FETCH_NUM);
//var_dump(count($emps));die();
//var_dump($emps);die();
$excel = $excel->data($emps);
}
// Output
$filePath = $excel->output();
result

Reference article :http://www.884358.com/php-xlswriter
Document address :https://xlswriter-docs.viest.me/zh-cn/an-zhuang/
边栏推荐
- [2022 ciscn] replay of preliminary web topics
- Jenkins远程构建项目超时的问题
- What is the difference between TCP and UDP?
- Make a bat file for cleaning system garbage
- Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
- Zhilian + AV, AITO asked M7 to do more than ideal one
- URP - shaders and materials - light shader lit
- How to reduce inventory with high concurrency on the Internet
- gslx680触摸屏驱动源码码分析(gslX680.c)
- After the interview, the interviewer roast in the circle of friends
猜你喜欢
随机推荐
Kbu1510-asemi power supply special 15A rectifier bridge kbu1510
leetcode:105. Constructing binary trees from preorder and inorder traversal sequences
2、 Concurrent and test notes youth training camp notes
@component(““)
Leetcode-206. Reverse Linked List
Rust Versus Go(哪种是我的首选语言?)
Dynamics CRM server deployment - restore database prompt: the database is in use
../ And/
Live online system source code, using valueanimator to achieve view zoom in and out animation effect
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
A concurrent rule verification implementation
【斯坦福计网CS144项目】Lab3: TCPSender
[Stanford Jiwang cs144 project] lab3: tcpsender
Simple example of ros2 planning system plansys2
[unity] several ideas about circular motion of objects
Live broadcast platform source code, foldable menu bar
MobaXterm
【obs】win-capture需要winrt
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
外包幹了三年,廢了...

![[webrtc] m98 Screen and Window Collection](/img/b1/1ca13b6d3fdbf18ff5205ed5584eef.png)
![[SUCTF 2019]Game](/img/9c/362117a4bf3a1435ececa288112dfc.png)





