Export from database to CSV file method csv is what file format ADL file convert CSV CSV file format

Source: Internet
Author: User
Public functionExport(){//data from the database, in order to save memory, do not read the data once to the memory, a row of a sentence to read the handle//Output Excel file header, you can replace the user.csv with the file name you wantHeader (' Content-type:application/vnd.ms-excel ');Header (' content-disposition:attachment;filename= ' order.csv "');Header (' cache-control:max-age=0 ');$where=Array("Paid"=1,"Pay_type"=Array("NEQ","Offline"),"Status"=Array("LT",3),);$stmt= M ("Group_order"),Field("Order_id,order_name,num,price,total_money,contact_name,phone,zipcode,adress,wx_cheap,balance_pay,payment _money,tuan_type,pay_time,pay_type,third_id,is_mobile_pay,paid,status "),where($where),Order("order_id DESC"),Limit( +),Select();//Open PHP file handle, php://output indicates direct output to browser$fp= fopen (' Php://output ', ' A ');//Output Excel column name information$head= Array("Order Number","Order name","Purchase Quantity","Price","Total Price","Contact person name","Contact Phone","Postcode","Detailed Address","Preferential Amount","Balance Payment amount","Actual payment amount","Sale Type (2 is in kind)","Payment Time","Payment Type","third-party payment ID","Whether it is mobile payment","Whether to pay","Order Status");foreach($head as$i=$v) {//CSV Excel support GBK encoding, be sure to convert otherwise garbled$head[$i] = Iconv (' Utf-8 ', ' GBK ', $v);}//Writes data to a file handle via FputcsvFputcsv ($fp, $head);//Counter$cnt= 0;//Every $limit line, refresh the output buffer, not too big, not too small$limit=  -;//row-by-line extraction of data without wasting memory$count= count ($stmt); for($t=0;$t<$count;$t++) {$cnt++;off($limit== $cnt) { //Refresh the output buffer to prevent problems caused by excessive dataOb_flush ();Flush ();$cnt= 0;}$row= $stmt[$t];foreach($row as$i=$v) {if($i==' Pay_time '){$v=date ("Y-m-d,h:i:s",$v);}$row[$i] = Iconv (' Utf-8 ', ' GBK ', $v);}Fputcsv ($fp, $row);}fclose ($fp);}

The above describes the export from the database to the CSV file method, including the content of the CSV file, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.