Tips: PHP batch export csv file _ PHP Tutorial

Source: Internet
Author: User
Tips: export csv files in batches using PHP. Sometimes, when writing a program, the background requires that a large amount of data be imported into the database. for example, the computer examination result query and phone book data are generally stored in excel.

Sometimes, when writing a program, the background requires that a large amount of data be imported into the database. for example, the computer examination result query and phone book data are generally stored in excel, in this case, we can export the data to a csv file, and then use the following program to import data to the database in batches in the background.

The following is only the main program part for exporting csv files in PHP in batches:

  1. <? Php
  2. Function getmicrotime (){
  3. List ($ usec, $ sec) = explode ("", microtime ());
  4. Return (float) $ usec + (float) $ sec );
  5. }
  6. ?>

 
 
  1. <? Php
  2. $ Time_start = getmicrotime ();
  3. Include ("db. inc. php"); // connect to the database
  4. $ Db = new testcsv;
  5. ?>
  6. <? Php
  7. $ Handle = fopen (”test.csv "," r ");
  8. $ SQL = "insert into scores (idcard, names
    , Num, sex, nation, score) values ('";
  9. While ($ data = fgetcsv ($ handle, 1000, ",") {
  10. $ Num = count ($ data );
  11. For ($ c = 0; $ c <$ num; $ c ++ ){
  12. If ($ c ==$ num-1) {$ SQL = $ SQL. $ data [$ c]. "')"; break ;}
  13. $ SQL = $ SQL. $ data [$ c]. "','";
  14. }
  15. Print "<br> ";
  16. Echo $ SQL. "<br> ";
  17. $ Db-> query ($ SQL );
  18. Echo "SQL statement execution successful! <Br> ";
  19. $ SQL = "insert into scores (idcard, names
    , Num, sex, nation, score) values ('";
  20. }
  21. Fclose ($ handle );
  22. $ Time_end = getmicrotime ();
  23. $ Time = $ time_end-$ time_start;
  24. Echo "Program Execution time:". $ time. "Seconds ";
  25. ?>

We hope that you can master this technique by using the code example of batch export csv files in PHP.


Sometimes, when writing a program, the background requires that a large amount of data be imported into the database. for example, the computer examination result query and phone book data are generally stored in excel. at this time, we can...

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.