首页 > 代码库 > PHP导出大数据

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

直接下载:

<?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导出大数据