views:

57

answers:

2

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?

+2  A: 

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)";
ircmaxell
Yes, been camped on that page all night. Could have sworn I tried that variation.. Works, thanks for quick reply.
A: 

Yep the (order_num,pname) is invalid there's a different syntax for mapping fields to column names.

Khorkrak
It's valid (that's what the docs show at least, and that's not where the error is coming from)...
ircmaxell
As you said it goes at the end - not where he had it so syntax error.
Khorkrak