Background management system is always millions of data export, using Excel export can not be implemented at all, Excel only supports 65536,2007 and 2010 is 1048576, so no matter which can not meet the requirements, CSV to meet the requirements, do not limit the number of records, But a lot of data if the one-time processing of the export, will directly lead to memory overflow, the script stopped working, all the use of split-way export is more advantageous.
<?php error_reporting (0); Header ("Content-type:application/vnd.ms-excel"); Header ("Content-disposition:filename=". Iconv ("UTF-8", "GBK", "topic"). ". csv");//Connection Database $link = mysql_connect (' localhost ', ' root ', ' root ') or Die (' connection error ');//Select Database mysql_select_db ("BBS", $link) ;//Set CharSet mysql_query ("Set names UTF8");//Query function Functions get_res ($sql, $link) {$res = mysql_query ($sql, $link); if (! $res) { Die ("Operation failed". Mysql_error ());} $arr =array (), while ($row = Mysql_fetch_assoc ($res)) {$arr []= $row;} return $arr;} Total number of query records function Gettotalcount () {$result = mysql_query ("SELECT count (*) as Count from Medsci_edu_public_medsciedu_ Topic ", $link); return $result [' count '];} Open the PHP file handle, php://output indicates the direct output to the browser $fp = fopen (' php://output ', ' a '); Table header $column_name = Array (' topic_id ', ' cat_id ', ' user_id ', ' is_best ', ' is_top ', ' topic_title ', ' topic_content ', ' topic_ ' IMG ', ' hits ', ' Total_reply_count ', ' created_time ', ' last_updated_time ', ' topic_status ', ' last_reply_name '); Convert the Chinese title to code, otherwise garbled foreach ($column _name as $i = = $v) {$column _name[$i] = Iconv (' Utf-8 ', ' GBK ', $v); }//writes the title name through Fputcsv to the file handle Fputcsv ($FP, $column _name); $pagecount = 10000;//reads how many $totalcount = Gettotalcount ();//Total Records $sql = "SELECT * FROM Medsci_edu_public_medsciedu_topic "; for ($i =0; $i <intval ($totalcount/$pagecount) +1; $i + +) {$data = Get_res ($sql. "Limit". Strval ($i * $pagecount). ", {$PA Gecount} ", $link); foreach ($data as $item) {$rows = array (); foreach ($item as $v) {$rows [] = Iconv (' utf-8 ', ' GBK ', $v); } fputcsv ($fp, $rows); }//Destroys data storage variables already written to CSV, freeing up memory consumption unset ($DATA); Flush buffer Ob_flush (); Flush (); } exit;
Results test:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
PHP CSV bulk Data export segmentation processing