简体   繁体   中英

LOAD DATA INFILE not working with FIELDS TERMINATED BY

I'm using the sql below in a php script:

$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);
}

If I use $sql1 it properly brings 3 lines into the db (doesn't break them into proper fields). No error returned. If I use $sql2 it returns message:

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

I've tried different order, using ENCLOSED BY with it / instead of it.. Everything I can think of. Anyone have a suggestion?

Check the documentation ... The field declaration must come after the fields terminated by declaration:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM