欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php数据批量导入csv文件(示例)

程序员文章站 2022-06-11 12:25:01
...
  1. /**php导入csv文件到数据库***

  2. *同时计算程序执行时间
  3. ***/
  4. //定义获取时间函数
  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");//连接数据库
  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 if($c==$num-1){$sql=$sql.$data[$c]."\')";break;}
  17. $sql=$sql.$data[$c]."\',\'";
  18. }
  19. print "
    ";
  20. echo $sql."
    ";
  21. $db->query($sql);
  22. echo "SQL语句执行成功!
    ";
  23. $sql="insert into scores(idcard,names,num,sex,nation,score) values(\'";
  24. }
  25. fclose ($handle);
  26. $time_end = getmicrotime();
  27. $time = $time_end - $time_start;
  28. echo "程序执行时间:".$time."秒";
  29. ?>
复制代码