正确转义/格式化MySQL查询

时间:2016-04-04 11:49:23

标签: php mysql

$query = "  SELECT *
            FROM Catalog
            INTO OUTFILE './catalog.csv'
            OPTIONALLY ENCLOSED BY '\"'
            FIELDS TERMINATED BY ','
            LINES TERMINATED BY '\r\n'";

我得到的错误是OPTIONALLY ENCLOSED BY '\"'

存在问题

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTIONALLY ENCLOSED BY '"'

我在过去一小时内尝试了很多不同的变体,例如在查询周围使用单引号然后修改查询中的转义/引用,但我无法提供正确的格式。

2 个答案:

答案 0 :(得分:0)

需要将FIELDS TERMINATED BY ','部分放在OPTIONALLY ENCLOSED BY '\"'之前。

$query = "  SELECT *
            FROM Catalog
            INTO OUTFILE './catalog.csv'
            FIELDS TERMINATED BY ','
            OPTIONALLY ENCLOSED BY '\"'
            LINES TERMINATED BY '\r\n'";

答案 1 :(得分:0)

您的查询不对。应该是:

 "SELECT *
        FROM Catalog
        INTO OUTFILE '.\/catalog.csv'
         FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'           
        LINES TERMINATED BY '\r\n'";