简体   繁体   中英

PHP/Mysql Load Data Local Infile

I have tried everything that I can think of, but for some reason I cannot get this to work.

I have a piece of PHP code that loads a locally stored CSV file into a MySQL database. It works perfectly in one script:

$query = 'LOAD DATA LOCAL INFILE \''.$arp_input.'\' INTO TABLE arp_import FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\r\n\' (not_used,account_number,check_serial,issue_date,amount,transaction_code,additional_text)';

However, I need to do the same thing (load a CSV) in another script that does essentially the same thing, but generates a different format. Here is the query:

$query = 'LOAD DATA LOCAL INFILE \''.$ap_input.'\' INTO TABLE ap_import FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\r\n\' (account_number,check,paiddate,amount)';

But when I run the second script with the above query, I get this 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 'check,paiddate,amount)' at line 1

When I take out the field listing at the end of the second query, it only imports ONE line from the file and that is it, even when I tried the \\n delimiter, \\r, and both \\r\\n.

I have tried everything that I can think of. These two PHP scripts are running on the same server and they are both accessing the same remote MySQL server. What am I missing here?

Check is a reserved word according to the MySQL documentation.

http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

check is a reserved word in MySQL ( http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html ). You should enclose it in back-ticks (`) in your sql sentence:

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