Php exports data in csv format

Source: Internet
Author: User
Php exports data in the format of csvphp & nbsp; exports data. the format is csv & nbsp; no phpexcel class. Is there any other method? How should I write a class by myself? Thank you! ------ Solution ------------------ $ fp & nbsp; fopen (csv file name, & nbsp; w); $ rs & nbsp; mysql_query (php exports data in csv format)
Php exports data in csv format without using the phpexcel class. Is there any other method? How should I write a class by myself? Thank you!
------ Solution --------------------
$ Fp = fopen ('csv filename ', 'w ');
$ Rs = mysql_query ('select * from tbl_name ');
While ($ row = mysql_fetch_assoc ($ rs ){
Fputcsv ($ fp, $ row );
}
Fclose ($ fp );

------ Solution --------------------


If (isset ($ _ POST ['import']) {

$ File = $ _ FILES ['csv _ goods '];

$ File_type = substr (strstr ($ file ['name'], '.'), 1 );

// Check the file format
If ($ file_type! = 'Csv '){
Echo 'file format is incorrect. please upload it again! ';
Exit;
}
$ Handle = fopen ($ file ['tmp _ name'], "r ");
$ File_encoding = mb_detect_encoding ($ handle );

// Check the file encoding
If ($ file_encoding! = 'Ascii '){
Echo 'file encoding error. please upload it again! ';
Exit;
}

$ Row = 0;
While ($ data = fgetcsv ($ handle, 1000 ,',')){
// Echo "$ row"; // you can know the total number of rows.
$ Row ++;
If ($ row = 1)
Continue;
$ Num = count ($ data );
// The data of each cell in each row is output in sequence.
For ($ I = 0; $ I <$ num; $ I ++ ){
Echo $ data [$ I]."
";
// Process the data here
}
}

Fclose ($ handle );
}

?>

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.