MySql包含双引号

时间:2014-01-10 22:43:22

标签: php mysql escaping

这个代码在php文件中有什么问题?

$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));

看起来我见过很多例子,但是我收到了错误:

  

解析错误:语法错误,第66行/users13/domains/efsowell.us/web/ed/JOCMemDB/CSV_MySQL.php中的意外T_CONSTANT_ENCAPSED_STRING

1 个答案:

答案 0 :(得分:2)

您可以从语法高亮显示您遇到问题。

$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));