Notepad++Good Luck To You!

从数据库导出百万级数据(CSV文件)
<?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"');
 
 
//打开php标准输出流
//以写入追加的方式打开
$fp = fopen('php://output', 'a');

//连接数据库
$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpwd = '';
$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 = ""; //筛选条件
 
//设置标题
$title = array('ID','姓名‘,'年龄','性别');
foreach($title as $key => $item) {
    $title[$key] =iconv("UTF-8", "GBK", $item);
}
//将标题写到标准输出中
fputcsv($fp, $title);

for($s = 1; $s <= $step; $s++) {
    $start = ($s - 1) * $nums;
    $result = mysqli_query($con,"SELECT * FROM `test` ".$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); //释放结果集资源

        //每1万条数据就刷新缓冲区
        ob_flush();
        flush();
    }
}
//断开连接
mysqli_close($con);


«    2023年7月    »
12
3456789
10111213141516
17181920212223
24252627282930
31
TOP 搜索
TOP 控制面板
您好,欢迎到访网站!
  查看权限
TOP 最新留言
    TOP 作者列表
    TOP 站点信息
    • 文章总数:163
    • 页面总数:0
    • 分类总数:6
    • 标签总数:20
    • 评论总数:0
    • 浏览总数:312004
    召唤伊斯特瓦尔