php 文本文件操作读取txt文件保存到mysql数据库

实例1、php 读txt文件插入数据库,代码如下:

  1. <?php
  2. $m = mysql_connect('127.0.0.1','root','') or die("Invalid query: " . mysql_error());
  3. mysql_select_db('discuz', $m) or die("Invalid query: " . mysql_error());
  4. $handle = fopen("aa.txt", "r");
  5. while (!feof($handle)) {
  6. $buffer = ($handle);
  7. $ss[] = explode('&nbsp', $buffer);
  8. }
  9. //var_dump($ss);
  10. mysql_query("set names 'utf8'",$m);
  11. foreach($ss as $k => $v){
  12. //addslashes($v);
  13. //foreach($v as $k = $value){
  14. //$vv = addslashes($value);
  15. //mysql_query("insert into match_view_test_2 (`kanum`, `username`, `mid`, `rank`, `num` ,`zunum` ,`sex` ,`yucolor` ,`eye` ,`backtime` ,`kongju` ,`fengspeed` )
  16. //values ('123','123', 20111014060110765 ,1,'123','123','123','123','123','123','123','123')") or die("Invalid query: " . mysql_error());
  17. //}
  18. $kanum = $v[1];
  19. $username = $v[2];
  20. $rank = (int)$v[0];
  21. $num = $v[3];
  22. $zunum = $v[4];
  23. $sex = $v[5];
  24. //echo $sex;
  25. $yucolor = $v[6];
  26. //$eye = $v[7];
  27. $backtime = $v[8].' '.$v[9];
  28. $kongju = $v[10];
  29. $fengspeed = $v[11];
  30. mysql_query("insert into match_view_test (`kanum`, `username`, `mid`, `rank`, `num` ,`zunum` ,`sex` ,`yucolor` ,`eye` ,`backtime` ,`kongju` ,`fengspeed` )
  31. VALUES('$kanum','$username', 20111014060110765,$rank,'$num','$zunum','$sex','$yucolor','$eye','$backtime','$kongju','$fengspeed')") or die("Invalid query: " . mysql_error());
  32. }
  33. ?>

php读取txt文件组成SQL并插入数据库的代码.

先看数据结构,代码如下:

  1. -- 数据表结构:
  2. -- 100000_insert,1000000_insert
  3. CREATE TABLE `100000_insert` (
  4. `id` int(11) NOT NULL AUTO_INCREMENT,
  5. `parentid` int(11) NOT NULL,
  6. `name` varchar(255) DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
  9. 100000 (10万)行插入:Insert 100000_line_data use 2.5534288883209 seconds
  10. 1000000(100万)行插入:Insert 1000000_line_data use 19.677318811417 seconds

php文件,代码如下:

  1. <?php
  2. /**
  3. * $splitChar 字段分隔符
  4. * $file 数据文件文件名
  5. * $table 数据库表名
  6. * $conn 数据库连接
  7. * $fields 数据对应的列名
  8. * $insertType 插入操作类型,包括INSERT,REPLACE
  9. */
  10. function loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields=array(),$insertType='INSERT'){
  11. if(emptyempty($fields)) $head = "{$insertType} INTO `{$table}` VALUES('";
  12. else $head = "{$insertType} INTO `{$table}`(`".implode('`,`',$fields)."`) VALUES('"; //数据头
  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. //处理数据体,二者顺序不可换,否则空格或Tab分隔符时出错
  25. $sqldata = preg_replace('/(s*)(n+)(s*)/i',''),('',$sqldata); //替换换行
  26. $sqldata = $specialFunc($splitChar,$replace,$sqldata); //替换分隔符
  27. $query = $head.$sqldata.$end; //数据拼接
  28. if(mysql_query($query,$conn)) return array(true);
  29. else {
  30. return array(false,mysql_error($conn),mysql_errno($conn));
  31. }
  32. }
  33. //调用示例1
  34. require 'db.php';
  35. $splitChar = '|'; //竖线
  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!<br/>';
  42. }else {
  43. echo 'Failed!--Error:'.array_shift($result).'<br/>';
  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. //调用示例2
  60. require 'db.php';
  61. $splitChar = ' '; //空格
  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!<br/>';
  68. }else {
  69. echo 'Failed!--Error:'.array_shift($result).'<br/>';
  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. //调用示例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!<br/>';
  94. }else {
  95. echo 'Failed!--Error:'.array_shift($result).'<br/>';
  96. }
  97. /* sqldata3.txt
  98. Aston DB19 2009
  99. Aston DB29 2009
  100. Aston DB39 2009
  101. */
  102. //调用示例3
  103. require 'db.php';
  104. $splitChar = ' '; //Tab
  105. $file = 'sqldata3.txt';
  106. $fields = array('id','value');
  107. $table = 'notExist'; //不存在表
  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).'<br/>';
  113. }
  114. //附:db.php
  115. /* //注释这一行可全部释放
  116. ?>
  117. <?php
  118. static $connect = null;
  119. static $table = 'jilian';
  120. if(!isset($connect)) {
  121. $connect = mysql_connect("localhost","root","");
  122. if(!$connect) {
  123. $connect = mysql_connect("localhost","Zjmainstay","");
  124. }
  125. if(!$connect) {
  126. die('Can not connect to database.Fatal error handle by /test/db.php');
  127. }
  128. mysql_select_db("test",$connect);
  129. mysql_query("SET NAMES utf8",$connect);
  130. $conn = &$connect;
  131. $db = &$connect;
  132. }
  133. ?>

如果出现MySQL server has gone away 是因为导入数据包过大,导入mysql挂机了,我们解决方法是修改my.ini/my.cnf max_allowed_packet=20M.