The phpfputcsv function exports a mysql database to an excel file.

Source: Internet
Author: User
The fputcsv () function format rows as CSV and writes an open file. This function returns the length of the written string. If an error occurs, false is returned. (fputcsv () format a row (transmitted using the fields array)... the fputcsv () function format rows as CSV and writes an open file. This function returns the length of the written string. If an error occurs, false is returned.

Note: fputcsv () format a row (transmitted using the fields array) into CSV format and write the file specified by file.

The PHP instance code is as follows:

 Query ($ SQL); // open the php file handle. php Tutorial: // output indicates that the file is directly output to the browser $ fp = fopen ('php: // output ', 'A'); // output Excel column name information $ head = array ('name', 'gender ', 'age', 'Email', 'tel ','...... '); Foreach ($ head as $ I => $ v) {// CSV Excel supports GBK encoding and must be converted, otherwise, garbled code $ head [$ I] = iconv ('utf-8', 'gbk', $ v);} // writes data to the file handle fputcsv through fputcsv ($ fp, $ head); // counter $ cnt = 0; // refresh the output buffer every $ limit row. do not set it to too large or too small $ limit = 100000; // Extract data row by row without wasting memory while ($ row = $ stmt-> fetch (Zend_Db: FETCH_NUM) {// open source code phprm.com $ cnt ++; if ($ limit = $ cnt) {// refresh the output buffer to prevent problems caused by excessive data. ob_flush (); flush (); $ cnt = 0 ;} foreach ($ row as $ I =>$ v) {$ row [$ I] = iconv ('utf-8', 'gbk', $ v );} fputcsv ($ fp, $ row );}


Article link:

Save this article for favorites!

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.