PHP fputcsv function to export MySQL database to excel file

Source: Internet
Author: User
Tags foreach php file php tutorial stmt mysql database

The Fputcsv () function formats the row as a CSV and writes an open file.

The function returns the length of the write String. If an error occurs, it returns false ...


Description
Fputcsv () formats a row (passed in fields array) in CSV format and writes to the file specified by file.

Output Excel file headers, you can replace user.csv with the file name you want
Header (' Content-type:application/vnd.ms-excel ');
Header (' Content-disposition:attachment;filename= "user.csv");
Header (' cache-control:max-age=0 ');

Get the data from the database tutorial, in order to save memory, do not read the data to memory at once, one line in the handle can be read
$sql = ' SELECT * from tbl where ... ';
$stmt = $db->query ($sql);

Open php file Handle, PHP Tutorial://output Direct output to browser
$fp = fopen (' php://output ', ' a ');

Output Excel Column name information
$head = Array (' name ', ' gender ', ' age ', ' Email ', ' telephone ', ' ... ');
foreach ($head as $i => $v) {
CSV Excel supports GBK encoding, be sure to convert, otherwise garbled
$head [$i] = iconv (' utf-8 ', ' GBK ', $v);
}

Write data to a file handle by fputcsv
Fputcsv ($fp, $head);

Counter
$cnt = 0;
Every $limit line, refresh the output buffer, not too big, not too small
$limit = 100000;

Fetching data line by row without wasting memory
while ($row = $stmt->fetch (zend_db::fetch_num)) {

$cnt + +;
if ($limit = = $cnt) {//Refresh the output buffer to prevent problems caused by too much data
Ob_flush ();
Flush ();
$cnt = 0;
}

foreach ($row as $i => $v) {
$row [$i] = iconv (' utf-8 ', ' GBK ', $v);
}
Fputcsv ($fp, $row);
}

Parameters Description
File Necessary. Specify the open file to write to.
Fields Necessary. An array that prescribes the data to be obtained from.
Seperator Optional. The character that prescribes the field delimiter. The default is a comma (,).
Enclosure Optional. The characters that specify the field wrap character. The default is double quotes ".

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.