Home  >  Article  >  Backend Development  >  PHP code example to export millions of data from the database (CSV file)

PHP code example to export millions of data from the database (CSV file)

不言
不言forward
2018-11-17 16:56:233023browse

The content of this article is about the code example (CSV file) of PHP exporting millions of data from the database. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

Replace the database connection information, query conditions, and title information with real data to use.

 $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);

The above is the detailed content of PHP code example to export millions of data from the database (CSV file). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete