2014-01-10 120 views
0

這段代碼在php文件中出現了什麼問題?MySql由雙引號括起

$q = "LOAD DATA LOCAL INFILE '$file' INTO TABLE $table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES" ; 
echo "LOAD DATA query = " . $q . "<br />"; 
mysqli_query($DBlink, $q) or die (mysqli_error($DBlink)); 

它看起來像很多例子我見過,但是我得到的錯誤:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /users13/domains/efsowell.us/web/ed/JOCMemDB/CSV_MySQL.php on line 66

回答

1

您可以從語法高亮,你有一個問題看。

$q = "LOAD DATA LOCAL INFILE '$file' 
INTO TABLE $table FIELDS TERMINATED BY ',' 
OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' 
IGNORE 1 LINES" ; 
echo "LOAD DATA query = " . $q . "<br />"; 
mysqli_query($DBlink, $q) or die (mysqli_error($DBlink)); 

您需要在字符串中轉義引號。如果您的字符串是雙引號,則必須使用雙引號。如果是單引號,單引號也是一樣。

您還可以利用帶雙引號的變量插值。

$q = "LOAD DATA LOCAL INFILE '{$file}' 
INTO TABLE $table FIELDS TERMINATED BY ',' 
OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' 
IGNORE 1 LINES" ; 
echo "LOAD DATA query = {$q}<br />"; //<-- This line. 
mysqli_query($DBlink, $q) or die (mysqli_error($DBlink)); 
+0

謝謝傑西卡!我以爲我嘗試了逃跑,但也許我認爲特殊的字符是雙打逃脫的。無論如何,'\''的作品! –

相關問題