標籤:
儲存到本地
<?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匯出大資料