PHP匯出大資料

來源:互聯網
上載者:User

標籤:

儲存到本地

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

直接下載:

<?phpheader(‘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);?>

 

PHP匯出大資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.