我有一个完整的 PHP 脚本,它处理上传的文本文件并将数据导入 MySQL 表。
每次上传文本文件时:
- 现有表被删除
- 创建一个同名的新表
- 并将所有新数据插入到新表中。
文本文件是由公司的内部数据库软件创建的,这就是这个过程的原因。
文本文件中的字段/值由反斜杠分隔,脚本成功地将其分解:
$this_array = explode("\\", $this_string);
然后它通过在每行末尾查找换行符来结束数组的该部分(数组该部分的末尾将是数据库行的末尾)。例如。在示例文本文件(如下)中,换行符出现在'Row1Data for colname4'
. 数组的那部分应该成为数据库表中的一行。
然而问题是脚本吐出这个错误:
Error #1136:Column count doesn't match value count at row 1
这是由于文本文件中的多个段落在它们的末尾也有换行符(参见下面的示例文本文件)。
问题:我不知道如何避免将段落的结尾解释为数组的行/部分的结尾。如何继续对多段文本使用此过程,而不会将它们解释为数组该部分的结尾?
示例文本文件内容:
colname1name\colname2name\colname3name\colname4name
Data for colname1\Row1Data for colname2\This is a Row1 Paragraph to go in colname3
This is another Row1 Paragraph to go in colname3
This is yet another Row1 Paragraph to go in colname3\Row1Data for colname4
Row2Data for colname1\Row2Data for colname2\This is a Row2 Paragraph 1 to go in colname3
This is another Row2 Paragraph to go in colname3
This is yet another Row2 Paragraph to go in colname3\Row2Data for colname4
脚本中有很多代码,但我认为这是最相关的部分:
//
//---------------------------------------create table--------------------------------
//
$text_string="CREATE TABLE `area` (";
//loop thru names
for ($n=0; $n< count($name_array); $n++){
$name_array[$n]=trim($name_array[$n]);//trim needed here
if($name_array[$n]=='population'){//population field has to be INT
$text_string.= "`".$name_array[$n]."` INT(8) NOT NULL,";
}elseif($name_array[$n]=='towndescription'){//description field has to be TEXT
$text_string.= "`".$name_array[$n]."` TEXT NOT NULL,";
}else{
$text_string.= "`".$name_array[$n]."` varchar(250) NOT NULL default '',";
}
}
//remove last comma
$string_len=strlen($text_string);
$string_len=$string_len-1;
$text_string=substr($text_string,0,$string_len);
//
$text_string.= ") ENGINE=MyISAM ";
$db_sql_query = $text_string;
$db_result = @mysql_query($db_sql_query, $db_connection) or die ("Error #" . mysql_errno() . ":" . mysql_error());
print $text_string."<BR><BR>";//////////
//
//----------------------------------------------------------------------------------------
//
//now loop thru $array
//
for ($n=1; $n<count($array) ; $n++){
$text_string= "INSERT INTO `area` VALUES (";
//for each line explode
$this_string=$array[$n];
$this_array = explode("\\", $this_string);
for ($i=0; $i< count($this_array); $i++){
//replace ' with html code - ’
$this_item=$this_array[$i];
$this_item=trim($this_item);//trim needed here
$this_item = str_replace("&", "and","$this_item" );
$this_item = str_replace("'", "’","$this_item" );
$this_item = str_replace("\"", "","$this_item" );//escaped "
//$this_item = str_replace(" ", "%20","$this_item" );
$text_string.= " '".$this_item."' ,";
}
//remove last comma
$string_len=strlen($text_string);
$string_len=$string_len-1;
$text_string=substr($text_string,0,$string_len);
//
$text_string.= ") ";
$db_sql_query = $text_string;
$db_result = @mysql_query($db_sql_query, $db_connection) or die ("Error #" . mysql_errno() . ":" . mysql_error());
print $text_string;//////////
}
任何帮助将不胜感激!