2

想知道是否有人可以在此找到错误:

    $sqlinsert = "LOAD DATA LOCAL INFILE 
\'$target_path\' INTO TABLE 'db_usergroup' 
FIELDS TERMINATED BY \',\' ENCLOSED BY \'\"\' ESCAPED BY \'\\\\\' 
LINES TERMINATED BY \'\\r\\n\'";

$target_path其确切名称略有不同,但示例如下:

temp/24_09_12_16_57_27_invoice_example.csv

我收到此错误:

    An error occurred. The file could not be imported. 
Error with MySQL Query: 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 
'\'temp/24_09_12_16_57_27_invoice_example.csv' INTO TABLE 
'db_usergroup' FIELDS T' at line 1

非常感谢所有帮助。

4

2 回答 2

0

为什么要转义目标路径中的引号?我不认为这是必要的。

您通常可以通过检查 之后的前几个字符来准确判断错误是什么for the right syntax to use near,这似乎表明反斜杠有问题。

尝试:

$sqlinsert = "LOAD DATA LOCAL INFILE '$target_path' INTO TABLE ...
于 2012-09-24T05:11:32.250 回答
0

'db_usergroup'是无效的。单引号将该'表名转换为一个字符串,MySQL 将使用该字符串。您不需要引用/转义表或字段名称,除非它们是保留字。为此,您使用反引号,例如

LOAD DATA ... `db_usergroup` ...
              ^--          ^--

跟进:

$sqlinsert = <<<EOL
LOAD DATA LOCAL INFILE '$target_path'
INTO TABLE db_usergroup
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\' 
LINES TERMINATED BY '\r\n'
EOL;
于 2012-09-24T05:23:32.020 回答