LOAD DATA INFILE不与FIELDS TERMINATED BY一起使用

时间:2010-06-17 15:50:52

标签: php mysql load-data-infile file-io

我在php脚本中使用下面的sql:

$sql1 = "LOAD DATA LOCAL INFILE 'test1.csv' INTO TABLE number1 (order_num,pname)";
$sql2 = "LOAD DATA LOCAL INFILE 'test1.csv' INTO TABLE number1 (order_num,pname) FIELDS TERMINATED BY ':'";
if ($result = $mysqli->query($sql)) {
    printf("<br>Section 4: %s",$mysqli->error);
    printf("|$result|$table");
} else {
    printf("<br>Section 5: %s",$mysqli->error);
}

如果我使用$sql1它会正确地将3行引入数据库(不会将它们分成正确的字段)。没有错误返回。如果我使用$sql2,则返回消息:

"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 'FIELDS TERMINATED BY ':'.."

我尝试了不同的顺序,使用ENCLOSED BY和/而不是它。我能想到的一切。有人有建议吗?

2 个答案:

答案 0 :(得分:5)

检查documentation ...字段声明必须在声明终止的字段之后:

$sql2 = "LOAD DATA LOCAL 
            INFILE 'test1.csv' 
            INTO TABLE number1 
            FIELDS TERMINATED BY ':'
            (order_num,pname)";

答案 1 :(得分:-1)

是(order_num,pname)无效,将字段映射到列名的语法不同。