Batch import of php Data into csv files (example)

Source: Internet
Author: User
Batch import of php Data into csv files (example)

  1. /** Import csv files to the database in php ***

  2. * Calculate the program execution time simultaneously.
  3. ***/
  4. // Define a time function
  5. Function getmicrotime (){
  6. List ($ usec, $ sec) = explode ("", microtime ());
  7. Return (float) $ usec + (float) $ sec );
  8. }

  9. $ Time_start = getmicrotime ();

  10. Include ("db. inc. php"); // connect to the database
  11. $ Db = new testcsv;

  12. $ Handle = fopen ("test.csv", "r ");

  13. $ SQL = "insert into scores (idcard, names, num, sex, nation, score) values (\'";
  14. While ($ data = fgetcsv ($ handle, 1000 ,",")){
  15. $ Num = count ($ data );
  16. For ($ c = 0; $ c <$ num; $ c ++ ){
  17. If ($ c ==$ num-1) {$ SQL = $ SQL. $ data [$ c]. "\ ')"; break ;}
  18. $ SQL = $ SQL. $ data [$ c]. "\ ', \'";
  19. }
  20. Print"
    ";
  21. Echo $ SQL ."
    ";
  22. $ Db-> query ($ SQL );
  23. Echo "SQL statement execution successful!
    ";
  24. $ SQL = "insert into scores (idcard, names, num, sex, nation, score) values (\'";
  25. }
  26. Fclose ($ handle );
  27. $ Time_end = getmicrotime ();
  28. $ Time = $ time_end-$ time_start;
  29. Echo "program execution time:". $ time. "seconds ";
  30. ?>

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.