Home  >  Article  >  Backend Development  >  PHP imports csv to sql file and automatically inserts it into the database (example)

PHP imports csv to sql file and automatically inserts it into the database (example)

WBOY
WBOYOriginal
2016-07-25 08:54:59949browse
  1. /**

  2. * php header import csv file
  3. * Editor: 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', '512M');
  10. define('DB_HOST', 'localhost');
  11. define('DB_USER', 'root');
  12. define('DB_PASS', '');
  13. define('DB_NAME', 'DB_XXX');

  14. //Batch import csv Format data and generate imported sql files

  15. //The csv file is listed as province (province, city, area, position, address), pay attention to correspond to the database fields
  16. $sql_list = array();
  17. foreach(array(
  18. 'chongqing. csv' => array('name' => 'Chongqing City','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` (`province`,`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("nn",$sql_list));
  42. if($write_result){
  43. echo $csv."File generation successful. n";
  44. }else{
  45. echo $csv."File writing failed! ! ! n";
  46. }

  47. }

  48. //Write to database (sql file generated by importing csv file by php)

  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 written successfully . nr";
  59. }else{
  60. echo 'Failed to write data';
  61. echo $csv_arr[$i]."Failed to write data! ! ! nr";
  62. }
  63. $i++;
  64. }
  65. mysql_close($lnk);
  66. unset($sql,$sql_list,$write_result,$csv_arr,$query_result,$lnk);
  67. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn