• PHP导出大数据


    保存到本地

    <?php
    // a db link for queries 
    $lh  = mysql_connect( '127.0.0.1', 'root', '' ); 
    // and a controller link 
    $clh = mysql_connect( '127.0.0.1', 'root', '', true ); 
    
    if ( mysql_select_db ( 'db', $lh ) ) 
    { 
        $qry    = "SELECT * FROM table limit 200000000"; 
        $rh     = mysql_unbuffered_query( $qry, $lh ); 
        $thread = mysql_thread_id ( $lh );
    
        $fp = fopen('file.csv', 'w');
        // 计数器
        $cnt = 0;
        // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
        $limit = 100000;
        while ( $res = mysql_fetch_row( $rh ) ) 
        { 
            $cnt ++;
            if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题
                ob_flush();
                flush();
                $cnt = 0;
            }
            fputcsv($fp, $res);
        }
        mysql_query( "KILL $thread", $clh );
        fclose($fp);
        unset($fp);
    }
    unset($lh, $clh);
    ?>

    直接下载:

    <?php
    
    header('pragma:public');
    header('Content-type:application/vnd.ms-excel;charset=utf-8;name="abc.csv"');
    header("Content-Disposition:attachment;filename=abcd.csv");//attachment新窗口打印inline本窗口打印 
    
    // a db link for queries 
    $lh  = mysql_connect( '127.0.0.1', 'root', '' ); 
    // and a controller link 
    $clh = mysql_connect( '127.0.0.1', 'root', '', true ); 
    
    if ( mysql_select_db ( 'db', $lh ) ) 
    { 
        $qry    = "SELECT * FROM table limit 200000000"; 
        $rh     = mysql_unbuffered_query( $qry, $lh ); 
        $thread = mysql_thread_id ( $lh );
    
        $fp     = fopen('php://output', 'a');
        // 计数器
        $cnt = 0;
        // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
        $limit = 100000;
        while ( $res = mysql_fetch_row( $rh ) ) 
        { 
            $cnt ++;
            if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题
                ob_flush();
                flush();
                $cnt = 0;
            }
            fputcsv($fp, $res);
        }
        mysql_query( "KILL $thread", $clh );
        fclose($fp);
        unset($fp);
    }
    unset($lh, $clh);
    ?>
  • 相关阅读:
    C语言I博客作业04
    C语言I博客作业02
    The First Assignment
    蒟蒻的长链剖分学习笔记(例题:HOTEL加强版、重建计划)
    分治FFT模板
    [Ynoi2016]掉进兔子洞 题解
    蒟蒻首开博客园博客QwQ
    bzoj4320 homework 题解
    [ZJOI2016]小星星(容斥+dp)
    【scoi2009】围豆豆(最短路模型)
  • 原文地址:https://www.cnblogs.com/Athrun/p/4184138.html
Copyright © 2020-2023  润新知