PHP Data Bulk Import CSV file (example)

Source: Internet
Author: User
  1. /**php Importing a CSV file to a database * * *

  2. * Simultaneous calculation of program execution time
  3. ***/
  4. Defining the Get 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 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 executed successfully!
    ";
  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. ?>

Copy Code
  • 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.