PHP代码中的报价问题

时间:2019-04-05 20:20:48

标签: php mysqli

基于“”或其他查询失败?

我尝试使用'“'或'\”'或'\“'

MySQL代码:

SELECT Name, Address, City FROM PERSONAL WHERE Name LIKE "A%" INTO OUTFILE '/tmp/output.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n';

当上述SQL语句在MySQL命令行中有效时。然后,我尝试将其转换为如下所示的PHP编码:

$filepath="/tmp/output.csv";
$query = "SELECT Name, Address, City FROM PERSONAL_DATA WHERE Name LIKE "A%" INTO OUTFILE '$filepath' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n'";

$result=mysqli_query($connection,$query) or die (mysqli_error());

在我看来,报价有问题。
当我使用OPTIONALLY ENCLOSED BY '"'

我尝试了OPTIONALLY ENCLOSED BY '\"''\\"'

查询测试失败。 PHP页面返回空白,有时会在日志中出现错误以进行解析。

2 个答案:

答案 0 :(得分:1)

由于使用双引号作为PHP字符串周围的定界符,因此需要对字符串内的所有双引号进行转义。您还可以在其中使用单引号,以避免不得不对其进行转义。

$query = "SELECT Name, Address, City FROM PERSONAL_DATA WHERE Name LIKE 'A%' INTO OUTFILE '$filepath' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n'";

答案 1 :(得分:0)

您可以在PHP中为字符串使用单引号,并转义字符串中使用的所有单引号。

onChange