Export csv data

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to export csv data. Thinkphp is usually used to process data, but the code is similar to the following code logic! Does not rely on any third-party class libraries
// Data Processing
Function exportData (){

$ Result = mysql_query ("select * from student ");
$ Str = "name, gender, age \ n ";
$ Str = iconv ('utf-8', 'gb2312', $ str );
While ($ row = mysql_fetch_array ($ result )){
$ Name = iconv ('utf-8', 'gb2312', $ row ['name']);
$ Sex = iconv ('utf-8', 'gb2312', $ row ['sex']);
$ Str. = $ name. ",". $ sex. ",". $ row ['age']. "\ n ";
}
$ Filename = date('ymd'0000.'.csv ';
Export_csv ($ filename, $ str );
}

// Output
Function export_csv ($ filename, $ data ){
Header ("Content-type: text/csv ");
Header ("Content-Disposition: attachment; filename =". $ filename );
Header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0 ');
Header ('expires: 0 ');
Header ('pragma: public ');
Echo $ data;
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.