Search code examples
phpmysqlload-data-infilefile-io

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?


Solution

  • 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)";