0

I have a text file "result.txt" that used pipes '|' to separate out the fields. I used PhpMyAdmin and successfully imported it to my table by specifying using "CSV LOAD DATA" and telling it the fields should be separated by '|'.

PhpMyAdmin also gave the full query for it, so I copied it and paste it into my php script, which looked like this:

 mysql_query("LOAD DATA LOCAL INFILE 'C:/wamp/www/TouchStone/result.txt' INTO TABLE customer_change FIELDS TERMINATED BY '|' ESCAPED BY '\\' LINES TERMINATED BY '\r\n' ")
 or die(mysql_error());

I will always receieve error saying:

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 ''' at line 2

I was wondering, since I copied exactly the same query generated by phpmyadmin, I think it will definitely work here. But why will such error happen?

I tried trimming the query to contain only "FIELDS TERMINATED BY " and it worked. But the database populated this way will contain incorrect data. So I am really wishing to learn why would the original longer query would fail?

Thanks.

0

2 Answers 2

2

You're using a double-quoted string, so the \r\n will be seen as literal carriage return and line feed characters. You'll need to double-escape them as well: \\r\\n.

The "on line 2" in the error message is evidence of this - there's no actual second line if your query, but because of the embedded newline/carriage return, there is once it gets to MySQL.

Sign up to request clarification or add additional context in comments.

2 Comments

I changed that to double-escape, but I got following error: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 ''customer_change' FIELDS TERMINATED BY '|' ESCAPED BY '\' LINES TERMINATED BY '\' at line 1
yeah, I am totally lost here too :(
0

Try this :

mysql_query("LOAD DATA LOCAL INFILE 'C:/wamp/www/TouchStone/result.txt' INTO TABLE customer_change FIELDS TERMINATED BY '|' ESCAPED BY '//' LINES TERMINATED BY '/r/n' ")
 or die(mysql_error());

1 Comment

I got different error:Field separator argument is not what is expected; check the manual

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.