php批量数据导出方案

批量导入参考https://blog.csdn.net/qq_20521363/article/details/102647756 

open($filename, ZipArchive::CREATE);   //打开压缩包
    foreach ($fileNameArr as $file) {
        $zip->addFile($file, basename($file));   //向压缩包中添加文件
    }
    $zip->close();  //关闭压缩包
    foreach ($fileNameArr as $file) {
        unlink($file); //删除csv临时文件
    }
    //输出压缩文件提供下载
    header("Cache-Control: max-age=0");
    header("Content-Description: File Transfer");
    header('Content-disposition: attachment; filename=' . basename($filename)); // 文件名
    header("Content-Type: application/zip"); // zip格式的
    header("Content-Transfer-Encoding: binary"); //
    header('Content-Length: ' . filesize($filename)); //
    @readfile($filename);//输出文件;

    unlink($filename); //删除压缩包临时文件

}


//生成器来缓存mysql查询结果,返回类型为数组
function query($sql,$limit,$fp){
    $con = mysqli_connect("localhost", "root", "root");
    if (!$con) {
        die('Could not connect: ' . mysqli_error());
    }
    mysqli_select_db($con, "test_batch");
    mysqli_query($con,'set names utf8');
//    print_r(mysqli_query($con, $sql,MYSQLI_USE_RESULT) ); mysqli_result Object 返回
    //该处用MYSQLI_USE_RESULT 就是不缓存结果集中,也是为了避免内存溢出,相当于mysql_unbuffered_query
    foreach (mysqli_query($con, $sql,MYSQLI_USE_RESULT) as $row ){  //
        yield $row; //生成器

    }
    $con->close();
}
$sqlCount= 1000000;
$sql = 'SELECT id,class,student FROM `test_yield`  where id <= '.$sqlCount;
//$sql = 'SELECT id,ArticleClassify,ArticleTitle FROM `test_yield`  where id < 20';
$mark = 'test';
putCsv($sql,$mark,$sqlCount);

?>
;

 

你可能感兴趣的:(PHP)