0
我使用上傳數據的.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);
}
}
}
你是如何將數據導入到MySQL,可以通過 'LOAD DATA INFILE ......'? – 2011-02-10 05:56:58
不,我要插入得到.csv文件記錄,並通過PHP – shazia 2011-02-10 06:04:20