1. 程式人生 > >php - 從資料庫匯出百萬級資料(CSV檔案)

php - 從資料庫匯出百萬級資料(CSV檔案)

將資料庫連線資訊、查詢條件、標題資訊替換為真實資料即可使用。

<?php
set_time_limit(0);
ini_set('memory_limit', '128M');
 
$fileName = date('YmdHis', time());
header('Content-Encoding: UTF-8');
header("Content-type:application/vnd.ms-excel;charset=UTF-8");
header('Content-Disposition: attachment;filename="' . $fileName
. '.csv"'); //開啟php標準輸出流 //以寫入追加的方式開啟 $fp = fopen('php://output', 'a'); //連線資料庫 $dbhost = '127.0.0.1'; $dbuser = 'root'; $dbpwd = ''; $con = mysqli_connect($dbhost, $dbuser, $dbpwd); if (mysqli_connect_errno()) { die('connect error'); } //選擇資料庫 $database = 'test'; mysqli_select_db($con
, $database); //設定編碼 mysqli_query($con, "set names UTF8"); //我們試著用fputcsv從資料庫中匯出1百萬的資料 //我們每次取1萬條資料,分100步來執行 //如果線上環境無法支援一次性讀取1萬條資料,可把$nums調小,$step相應增大。 $step = 100; $nums = 10000; $where = ""; //篩選條件 //設定標題 $title = array('ID','姓名‘,'年齡','性別'); foreach($title as $key => $item) { $title[$key] =iconv("UTF-8", "GBK", $item); } //將標題寫到標準輸出中 fputcsv($fp, $title); for($s = 1; $s <= $step; $s++) { $start = ($s - 1) * $nums; $result = mysqli_query($con,"SELECT * FROM `test` ".$where." ORDER BY `id` LIMIT {$start},{$nums}"); if($result) { while($row = mysqli_fetch_assoc($result)) { foreach($row as $key => $item) { //這裡必須轉碼,不然會亂碼 $row[$key] = iconv("UTF-8", "GBK", $item); } fputcsv($fp, $row); } mysqli_free_result($result); //釋放結果集資源 //每1萬條資料就重新整理緩衝區 ob_flush(); flush(); } } //斷開連線 mysqli_close($con);