php匯出百萬資料到csv

2022-06-20 07:30:17 字數 1823 閱讀 4996

<?php

set_time_limit(0); //

設定超時

ini_set('memory_limit', '100m'); //

設定最大使用的記憶體

header("content-type:text/csv");

header("content-disposition:attachment;filename=" . date('ymd'). '.csv');

header('cache-control:must-revalidate,post-check=0,pre-check=0');

header('expires:0');

header('pragma:public');

$out = fopen('php://output', 'w');

$bom = chr(0xef).chr(0xbb).chr(0xbf); //

防止亂碼

$func = function ($list

)

return

$arr

; }

//todo 注意我這裡都是用sql語句直接代替查詢

$list = select *from area;

$area = $func($list

);

$list = select *from greate;

$greate = $func($list

);

$list = select *from school;

$school = $func($list

);

ob_end_clean

();

ob_implicit_flush(5);

fputcsv(

$out, [$bom . '學生編號','學生姓名','所屬區域','所屬年級','所屬學校']);

//上面整理好了對應關係

do }

while(true

)

fclose($out

);

exit();

起因:有一次業務需求,需要匯出資料庫中的所有資料給介面下游,以便下游比對所有資料是否一致。

思考

jion與orm用哪個

用orm為什麼慢

用orm是否也可以很快

先說今天的主題

student table

字段型別

備註id

intstudent_no

string

學生編號

name

string

學生姓名

areaid

int所屬區域的id

gradeid

int年級id

schoolid

int學校id

area table (15條資料)

字段型別

備註id

intname

string

區域名稱

greate table (12條資料)

字段型別

備註id

intname

string

區域名稱

school table (100+條資料)

字段型別

備註id

intname

string

校園名稱

要求匯出所有內容到csv檔案中 格式要求:學生編號,學生姓名,所屬區域,所屬年級,所屬學校

PHP匯出資料到CSV檔案

後台往往需要匯出各種資料到 excel文件中。通常我們是匯出 csv檔案格式,php匯出函式參考 如下 匯出資料到csv檔案 param array data 二維陣列 模擬資料表記錄 param array titlelist 標題陣列列表 param string filename csv檔名 ...

PHP百萬級資料匯出csv格式OR文字格式

廢話不說,先來上乙個小小的demo header content type text csv header content transfer encoding binary for i 0 i 500000 i 將以上 儲存為乙個php檔案,然後執行,你會得到乙個50萬行記錄的檔案。當然副檔名是ph...

php 匯出大資料到csv檔案

set time limit 0 ini set memory limit 256m db this load database default true sql select from t mobile number section condition query db query sql res...