1

I am trying to set up a php script that collects a text file from an external ftp server and then imports that text file into a pre-formatted MySql table. When I do this manually via phpmyadmin there is an option available to define the character set of the incoming text file. I need to incorporate that step in the php script and define the character set is iso-8559-1. Following is part of my script that deals with the import - what do I need to add (and where) to define the character set in the same way as in my phpmyadmin?

mysql_select_db("mydatabase_db", $db);
$fileName="/home/mydomain/public_html/ImportsFolder/" . "IncomingFiles-" . $today . ".txt";
$query = <<<eof
LOAD DATA LOCAL INFILE '$fileName'
INTO TABLE filesincoming
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(id,file_url,file_type,file_index,file_caption,file_updated)
eof;
echo "<br />Populated files incoming.";

The script does run OK and imports the text file but it imports it as if character set is utf-8 and this causes some characters to be imported incorrectly into the table (eg a £ sign is imported as a question mark).

Any advice would be much appreciated. Many thanks in advance

2 Answers 2

0

From http://dev.mysql.com/doc/refman/5.1/en/load-data.html :

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
[...]
0

Some easy help here for formatting is you can cheat a little by using HeidiSQL. Once the file is created and tested and loading fine in the DB, you can you the query that the software is creating and save that. Saves a lot of time when you might have dozen of inputs to create as this software will do it automatically with Priorities, Insert, Truncating, etc. Basically everything listed above, but in a nice little GUI.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.