How to read a txt file in php to form SQL and insert it into the database

WBOY
Release: 2016-07-25 09:04:33
Original
956 people have browsed it
  1. /**

  2. * $splitChar field separator
  3. * $file data file file name
  4. * $table database table name
  5. * $conn database connection
  6. * $fields column name corresponding to the data
  7. * $insertType insert operation type, including INSERT, REPLACE
  8. * Collection and organization: bbs.it-home.org
  9. */
  10. function loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields=array(),$insertType=' INSERT'){
  11. if(empty($fields)) $head = "{$insertType} INTO `{$table}` VALUES('";
  12. else $head = "{$insertType} INTO `{$table}` (`".implode('`,`',$fields)."`) VALUES('"; //Data header
  13. $end = "')";
  14. $sqldata = trim(file_get_contents($file));
  15. if(preg_replace('/s*/i','',$splitChar) == '') {
  16. $splitChar = '/(w+)(s+)/i';
  17. $replace = "$1',' ";
  18. $specialFunc = 'preg_replace';
  19. }else {
  20. $splitChar = $splitChar;
  21. $replace = "','";
  22. $specialFunc = 'str_replace';
  23. }
  24. //Processing data body, both The order cannot be changed, otherwise there will be an error when using spaces or tab separators
  25. $sqldata = preg_replace('/(s*)(n+)(s*)/i',''),('',$sqldata); //Replace Line break
  26. $sqldata = $specialFunc($splitChar,$replace,$sqldata); //Replace the delimiter
  27. $query = $head.$sqldata.$end; //Data splicing
  28. if(mysql_query($query,$conn )) return array(true);
  29. else {
  30. return array(false,mysql_error($conn),mysql_errno($conn));
  31. }
  32. }
  33. //Call example 1
  34. require 'db.php';
  35. $ splitChar = '|'; //Vertical bar
  36. $file = 'sqldata1.txt';
  37. $fields = array('id','parentid','name');
  38. $table = 'cengji';
  39. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
  40. if (array_shift($result)){
  41. echo 'Success!
    ';
  42. }else {
  43. echo 'Failed !--Error:'.array_shift($result).'
    ';
  44. }
  45. /*sqlda ta1.txt
  46. |0|A
  47. |1|B
  48. |1|C
  49. |2| D
  50. -- cengji
  51. CREATE TABLE `cengji` (
  52. `id` int(11) NOT NULL AUTO_INCREMENT,
  53. `parentid` int(11) NOT NULL,
  54. `name` varchar(255) DEFAULT NULL,
  55. PRIMARY KEY ( `id`),
  56. UNIQUE KEY `parentid_name_unique` (`parentid`,`name`) USING BTREE
  57. ) ENGINE=InnoDB AUTO_INCREMENT=1602 DEFAULT CHARSET=utf8
  58. */
  59. //Call example 2
  60. require 'db.php' ;
  61. $splitChar = ' '; //space
  62. $file = 'sqldata2.txt';
  63. $fields = array('id','make','model','year');
  64. $table = 'cars ';
  65. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
  66. if (array_shift($result)){
  67. echo 'Success!
    ';
  68. }else {
  69. echo 'Failed!--Error:'.array_shift($result).'
    ';
  70. }
  71. /* sqldata2.txt
  72. Aston DB19 2009
  73. Aston DB29 2009
  74. Aston DB39 2009
  75. -- cars
  76. CREATE TABLE `cars` (
  77. `id` int(11) NOT NULL AUTO_INCREMENT,
  78. `make` varchar(16) NOT NULL,
  79. `model` varchar(16) DEFAULT NULL,
  80. `year` varchar(16) DEFAULT NULL,
  81. PRIMARY KEY (`id`)
  82. ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
  83. */
  84. //Call Example 3
  85. require 'db.php';
  86. $splitChar = ' '; //Tab
  87. $file = 'sqldata3.txt';
  88. $fields = array('id','make','model','year');
  89. $table = 'cars';
  90. $insertType = 'REPLACE';
  91. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields,$insertType);
  92. if (array_shift($result)){
  93. echo 'Success!
    ';
  94. }else {
  95. echo 'Failed!--Error:'.array_shift($result).'
    ';
  96. }
  97. /* sqldata3.txt
  98. Aston DB19 2009
  99. Aston DB29 2009
  100. Aston DB39 2009
  101. */
  102. //Call Example 3
  103. require 'db.php';
  104. $splitChar = ' '; //Tab
  105. $file = 'sqldata3.txt';
  106. $fields = array('id','value');
  107. $table = 'notExist'; //Table does not exist
  108. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
  109. if (array_shift($result)){
  110. echo 'Success!< ;br/>';
  111. }else {
  112. echo 'Failed!--Error:'.array_shift($result).'
    ';
  113. }

  114. / /Attachment: db.php

  115. /* //Comment this line to release all
  116. ?>
  117. static $connect = null;
  118. static $table = 'jilian';
  119. if(!isset($connect )) {
  120. $connect = mysql_connect("localhost","root","");
  121. if(!$connect) {
  122. $connect = mysql_connect("localhost","Zjmainstay","");
  123. }
  124. if(!$connect) {
  125. die('Can not connect to database.Fatal error handle by /test/db.php');
  126. }
  127. mysql_select_db("test",$connect);
  128. mysql_query("SET NAMES utf8 ",$connect);
  129. $conn = &$connect;
  130. $db = &$connect;
  131. }
  132. ?>
Copy code

Copy code The code is as follows:

  1. //*/

  2. Data table structure

  3. -- Data table structure:
  4. -- 100000_insert,1000000_insert
  5. CREATE TABLE `100000_insert` (
  6. `id` int(11) NOT NULL AUTO_INCREMENT,
  7. `parentid` int(11) NOT NULL,
  8. `name` varchar(255) DEFAULT NULL,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
  11. 100000 (100,000) row insertion: Insert 100000_line_data use 2.5534288883209 seconds 1000000 (1 million) row insertion: Insert 1000000_line_data use 19.67731 8811417 seconds
  12. //Possible error: MySQL server has gone away
  13. //Solution: Modify my.ini/my.cnf max_allowed_packet=20M

Copy code
Author: Zjmainstay


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!