单元格中的文本有“,”上传 .csv 不起作用
我用来上传数据的 .csv 文件的单元格中有“,”。我如何忽略单元格中的“,”,以便该单元格中的数据进入数据库的单个字段。
cell data="墙壁上有铅基油漆剥落,墙壁上长出霉菌。总结与铅和霉菌相关的健康危害,并描述可以采取哪些措施来控制这两个公共卫生问题。请务必用文献中的证据支持你的立场”
该文本应该出现在一个字段中,但由于文本中有一个“,”,所以它出现在 2 个字段中。
有谁知道解决这个问题。
感谢
PHP 代码
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "SampleDataTab.txt";
//echo $csvfile;
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "output.sql";
/********************************/
echo $csvfile;
if(!file_exists($csvfile)) {
echo "File not found. Make sure you specified the correct path.\n";
exit;
}
$file = fopen($csvfile,"r");
if(!$file) {
echo "Error opening data file.\n";
exit;
}
$size = filesize($csvfile);
if(!$size) {
echo "File is empty.\n";
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$con = mysql_connect($databasehost,$databaseusername) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
//'/\s/',
$records=0;
$lines = 0;
$queries = "";
$query="";
$linearray = array();
//array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
foreach(preg_split('/\n/',$csvcontent) as $line) {
echo"<BR>". $line."<BR>";
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
if($lines==1)
$line = str_replace("`","\'",$line);
else
$line = str_replace("'","\'",$line);
/*************************************/
$linearray = explode($fieldseparator,$line);
$stat= mysql_query("select status_id from status where status='$linearray[3]'");
$status=mysql_fetch_row($stat);
$wri= mysql_query("select user_id from writer where name='$linearray[4]'");
$writer=mysql_fetch_row($wri);
$due= date("Y-m-d H:i:s",strtotime($linearray[5]));
$ord_date=date("Y-m-d H:i:s",strtotime($linearray[6]));
if($writer[0]==null)
echo "Please insert writer in database. Cannot add record";
else
{
echo "<BR> category: ".$linearray[13]."<BR>";
if($lines!=1)
{
$query = "insert into `$databasetable` (website,order_id,status,writer_id,due_date,order_recieve_date,no_of_pages,customer_name,topic,details,category,education_level,format,citation_style,email,alt_email) values('$linearray[0]','$linearray[1]',$status[0],$writer[0],'$due','$ord_date',$linearray[8],'$linearray[10]','$linearray[11]','$linearray[12]','$linearray[13]','$linearray[14]','$linearray[17]','$linearray[18]','$linearray[19]','$linearray[20]');";
$queries .= $query . "\n";}
echo $query."<BR>";
$records+= mysql_query($query);
echo $records;
}
}
mysql_close($con);
if($save) {
if(!is_writable($outputfile)) {
echo "File is not writable, check permissions.\n";
}
else {
$file2 = fopen($outputfile,"w");
if(!$file2) {
echo "Error writing to the output file.\n";
}
else {
fwrite($file2,$queries);
fclose($file2);
}
}
}
The .csv file i am using to upload data has ',' in the cell. How can i ignore the ',' in cells so the data in that cell goes in a single field of my database.
cell data="There is lead-based paint peeling from the walls, and mold growing on the walls. Summarize the health hazards associated with lead and mold, and describe what may be done to control these two public health problems. Please be sure to support your position with evidence from the literature"
This text should go in one field but because there is a ',' in the text it is going in 2 fields.
Does anyone know a fix for this.
Thanks
PHP code
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "SampleDataTab.txt";
//echo $csvfile;
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "output.sql";
/********************************/
echo $csvfile;
if(!file_exists($csvfile)) {
echo "File not found. Make sure you specified the correct path.\n";
exit;
}
$file = fopen($csvfile,"r");
if(!$file) {
echo "Error opening data file.\n";
exit;
}
$size = filesize($csvfile);
if(!$size) {
echo "File is empty.\n";
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$con = mysql_connect($databasehost,$databaseusername) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
//'/\s/',
$records=0;
$lines = 0;
$queries = "";
$query="";
$linearray = array();
//array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
foreach(preg_split('/\n/',$csvcontent) as $line) {
echo"<BR>". $line."<BR>";
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
if($lines==1)
$line = str_replace("`","\'",$line);
else
$line = str_replace("'","\'",$line);
/*************************************/
$linearray = explode($fieldseparator,$line);
$stat= mysql_query("select status_id from status where status='$linearray[3]'");
$status=mysql_fetch_row($stat);
$wri= mysql_query("select user_id from writer where name='$linearray[4]'");
$writer=mysql_fetch_row($wri);
$due= date("Y-m-d H:i:s",strtotime($linearray[5]));
$ord_date=date("Y-m-d H:i:s",strtotime($linearray[6]));
if($writer[0]==null)
echo "Please insert writer in database. Cannot add record";
else
{
echo "<BR> category: ".$linearray[13]."<BR>";
if($lines!=1)
{
$query = "insert into `$databasetable` (website,order_id,status,writer_id,due_date,order_recieve_date,no_of_pages,customer_name,topic,details,category,education_level,format,citation_style,email,alt_email) values('$linearray[0]','$linearray[1]',$status[0],$writer[0],'$due','$ord_date',$linearray[8],'$linearray[10]','$linearray[11]','$linearray[12]','$linearray[13]','$linearray[14]','$linearray[17]','$linearray[18]','$linearray[19]','$linearray[20]');";
$queries .= $query . "\n";}
echo $query."<BR>";
$records+= mysql_query($query);
echo $records;
}
}
mysql_close($con);
if($save) {
if(!is_writable($outputfile)) {
echo "File is not writable, check permissions.\n";
}
else {
$file2 = fopen($outputfile,"w");
if(!$file2) {
echo "Error writing to the output file.\n";
}
else {
fwrite($file2,$queries);
fclose($file2);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
LOAD DATA INFILE
语法,请将ENCLOSED BY '"'
添加到您的查询中。文档:http://dev.mysql.com/doc/refman/5.1/en/load-data.html
If you are using the
LOAD DATA INFILE
syntax, addENCLOSED BY '"'
to your query.Docs: http://dev.mysql.com/doc/refman/5.1/en/load-data.html