我有一个完整的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;//////////
}
任何帮助将不胜感激!
答案 0 :(得分:0)
在我看来,你可以在反斜杠上爆炸,然后根据需要抓取每一列,并假设所有列都将提供给一行。所以,你抓住第1,2,3和4列 - 然后在得到第四列时,写下行,然后重新启动。
以下是一些代码:
<?php
$text = "
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
";
$lines = explode('\\', $text);
$totalCols = 4;
$currentCol = 0;
$currentRow = 0;
foreach ($lines as $line)
{
echo $currentCol . '/' . $currentRow . ': ' . $line . "\n";
$currentCol++;
if ($currentCol == $totalCols)
{
$currentCol = 0;
$currentRow++;
}
}
?>
这将输出:
0/0: Data for colname1
1/0: Row1Data for colname2
2/0: 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
3/0: Row1Data for colname4
0/1: Row2Data for colname1
1/1: Row2Data for colname2
2/1: 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
3/1: Row2Data for colname4
Here's a live demo你可以玩。
我不得不调整你的数据以使其正常工作 - “colname4的Row1Data”没有反斜杠终止符。
答案 1 :(得分:0)
感谢所有在这里提供帮助的人。经过更多的研究后,我开始意识到,因为我导入了一个文本文件,所以在将文本上传到数据库之前将文本转换为CSV是有意义的(这意味着要删除所有这些斜杠)。
一旦我确保文本文件中的文本格式正确,我就使用Jay Williams的脚本将逗号分隔文件转换为关联数组。第一行应包含数组键。
这一切都意味着因为文本的多个段落都附有一对引号,所有段落都被我的脚本正确识别(稍加调整),而不会将它们解释为该部分的结尾数组。