技巧分享 PHP大量匯出csv檔案_PHP教程

來源:互聯網
上載者:User
對於

有時寫程式時後台要求把大量資料匯入資料庫中,比如電腦考試成績的查詢、電話簿的資料等一般都是存放在excel中的,這時我們可把資料匯出成csv檔案,然後通過以下程式即可在後台大量匯入資料到資料庫中。

下面只是PHP大量匯出csv檔案的主要程式部分:

  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”);//串連資料庫
  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語句執行成功!< 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 “程式執行時間:”.$time.”秒”;
  25. ?>

希望大家通過以上PHP大量匯出csv檔案的程式碼範例能夠掌握這一技巧。


http://www.bkjia.com/PHPjc/446090.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446090.htmlTechArticle對於 有時寫程式時後台要求把大量資料匯入資料庫中,比如電腦考試成績的查詢、電話簿的資料等一般都是存放在excel中的,這時我們可...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.