Php imports csv files to SQL files and inserts them into the database automatically (example)

Source: Internet
Author: User
Php imports csv files to SQL files and inserts them into the database automatically (example)

  1. /**

  2. * Import a csv file using the php header
  3. * Edit: bbs.it-home.org
  4. */
  5. Header ('content-type: text/html; charset = utf-8 ;');
  6. @ Date_default_timezone_set ('Asia/Shanghai ');
  7. @ Error_reporting (E_ALL );
  8. @ Ini_set ('display _ errors ', 'on ');
  9. @ Ini_set ('memory _ limit ', '12m ');
  10. Define ('Db _ host', 'localhost ');
  11. Define ('Db _ user', 'root ');
  12. Define ('Db _ pass ','');
  13. Define ('Db _ name', 'DB _ XXX ');

  14. // Import csv data in batches and generate an imported SQL file

  15. // The csv file column is province (province, city, area, position, address). Be sure to match the database field
  16. $ SQL _list = array ();
  17. Foreach (array (
  18. 'Chongqing.csv '=> array ('name' => 'Chongqing', 'sort '=> 4 ),
  19. 'Guizhou.csv '=> array ('name' => 'Guizhou province', 'sort '=> 24 ),
  20. 'Shanghai.csv '=> array ('name' => 'Shanghai', 'sort' => 2 ),
  21. 'Sichuan.csv '=> array ('name' => 'Sichuan province', 'sort '=> 23 ),
  22. 'Yunnan.csv '=> array ('name' => 'Yunnan province', 'sort '=> 25)
  23. ) As $ filename => $ _ data ){
  24. $ SQL = $ SQL _start = "INSERT INTO 'tbl _ data' ('Vince ', 'city', 'area', 'position', 'address', 'sort ') VALUES ";
  25. $ Handle = fopen ($ filename, 'r ');
  26. $ I = 0;
  27. Echo'
    ';
  28. While ($ row = fgetcsv ($ handle, 1000 ,',')){
  29. If (! $ Row) continue;
  30. If ($ I <1000000 ){
  31. $ SQL. = "('". iconv ('gbk', 'utf-8', trim ($ row [0]). "','". iconv ('gbk', 'utf-8', trim ($ row [1]). "','". iconv ('gbk', 'utf-8', trim ($ row [2]). "','". iconv ('gbk', 'utf-8', trim ($ row [3]). "','". iconv ('gbk', 'utf-8', trim ($ row [4]). "','". $ _ data ['sort ']. "'),";
  32. } Else {
  33. Break;
  34. }
  35. $ I ++;
  36. }
  37. $ SQL = rtrim ($ SQL ,',');
  38. $ SQL _list [] = $ SQL;
  39. $ Csv = 'csv _ import _ '. $ filename.' _ '. date ('Y-m-D').'. SQL ';
  40. $ Csv_arr [] = $ csv;
  41. $ Write_result = file_put_contents ($ csv, implode ("\ n", $ SQL _list ));
  42. If ($ write_result ){
  43. Echo $ csv. "file generated successfully. \ N ";
  44. } Else {
  45. Echo $ csv. "file writing failed !!! \ N ";
  46. }

  47. }

  48. // Write data to the database (php imports the SQL file generated by the csv file)

  49. $ Lnk = mysql_connect (DB_HOST, DB_USER, DB_PASS );
  50. Mysql_select_db (DB_NAME, $ lnk );
  51. Mysql_query ('set SQL _MODE = "" ', $ lnk );
  52. Mysql_query ('set NAMES utf8', $ lnk );
  53. $ I = 0;
  54. Foreach ($ SQL _list as $ SQL ){
  55. $ Query_result = mysql_query ($ SQL, $ lnk );
  56. If ($ query_result ){
  57. Echo 'write data :';
  58. Echo $ csv_arr [$ I]. "data is successfully written. \ N \ r ";
  59. } Else {
  60. Echo 'data writing failed ';
  61. Echo $ csv_arr [$ I]. "data writing failed !!! \ N \ r ";
  62. }
  63. $ I ++;
  64. }
  65. Mysql_close ($ lnk );
  66. Unset ($ SQL, $ SQL _list, $ write_result, $ csv_arr, $ query_result, $ lnk );
  67. ?>

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.