PHP中的语法错误,意外的T_CONSTANT_ENCAPSED_STRING

时间:2010-04-27 07:09:25

标签: php mysql sql into-outfile

mysql_connect("localhost","root","");
mysql_select_db("hitnrunf_db");

$result=mysql_query("select * from jos_users INTO OUTFILE 'users.csv' FIELDS ESCAPED BY '""' TERMINATED BY ',' ENCLOSED BY '"'
 LINES TERMINATED BY '\n' ");

header("Content-type:  text/plain");
header("Content-Disposition: attachment; filename=your_desired_name.xls");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

在上面的代码中查询字符串,即字符串mysql_quey

我们收到以下错误

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\wamp\www\samples\mysql_excel\exel_outfile.php on line 8

在查询字符串'\ n'中,包机未识别为字符串,这就是上述错误获取的原因

1 个答案:

答案 0 :(得分:7)

您需要将双引号转义为:\"而不是""

$result=mysql_query("select * from jos_users INTO OUTFILE 'users.csv' FIELDS ESCAPED BY '\"' TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' ");

未转义的"会提前终止该字符串。

示例:

这是不正确的:"A " is a double quote"
这是正确的:"A \" is a double quote"