php使用cvs导出百万条数据,大量数据

MySQL

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL DEFAULT '',
  `age` tinyint(3) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

测试数据

CREATE DEFINER=`root`@`localhost` PROCEDURE `zqtest`()
begin
declare i int default 0;
set i=0;
start transaction;
while i<100000 do
insert into user (name,age) values('test',i);
set i=i+1;
end while;
commit;
end;

PHP

set_time_limit(0);
ini_set('memory_limit', '128M');
 
$fileName = date('YmdHis', time());
header('Content-Encoding: UTF-8');
header("Content-type:application/vnd.ms-excel;charset=UTF-8");
header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
//注意,数据量在大的情况下。比如导出几十万到几百万,会出现504 Gateway Time-out,请修改php.ini的max_execution_time参数
//打开php标准输出流以写入追加的方式打开
$fp = fopen('php://output', 'a');
//连接数据库
$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpwd = 'root';
$con = mysqli_connect($dbhost, $dbuser, $dbpwd);
if (mysqli_connect_errno())
    die('connect error');
 
$database = 'test';//选择数据库
mysqli_select_db($con, $database);
mysqli_query($con, "set names UTF8");//如果需要请设置编码
 
//用fputcsv从数据库中导出1百万的数据,比如我们每次取1万条数据,分100步来执行
//一次性读取1万条数据,也可以把$nums调小,$step相应增大。
$step = 100;
$nums = 10000;
$where = "where 1=1"; //筛选条件,可自行添加
 
//设置标题
$title = array('id', '姓名', '年龄'); //注意这里是小写id,否则ID命名打开会提示Excel 已经检测到"xxx.xsl"是SYLK文件,但是不能将其加载: CSV 文或者XLS文件的前两个字符是大写字母"I","D"时,会发生此问题。
foreach ($title as $key => $item)
    $title[$key] = iconv("UTF-8", "GB2312//IGNORE", $item);
 
fputcsv($fp, $title);
 
for ($s = 1; $s <= $step; $s++) {
    $start = ($s - 1) * $nums;
    $result = mysqli_query($con, "SELECT id,name,age FROM `user` " . $where . " ORDER BY `ID` LIMIT {$start},{$nums}");
    if ($result) {
        while ($row = mysqli_fetch_assoc($result)) {
            foreach ($row as $key => $item)
                $row[$key] = iconv("UTF-8", "GBK", $item); //这里必须转码,不然会乱码
            fputcsv($fp, $row);
        }
        mysqli_free_result($result); //释放结果集资源
        ob_flush();  //每1万条数据就刷新缓冲区
        flush();
    }
}
mysqli_close($con);//断开连接
原文地址:https://www.cnblogs.com/wangzhaobo/p/13671951.html