Import csv file to MySQL with two different fields terminate

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
krasi_e_d
Forum Newbie
Posts: 20
Joined: Mon Feb 27, 2012 5:00 am

Import csv file to MySQL with two different fields terminate

Post by krasi_e_d »

Hello,

How to import csv file with two different field terminated. I use this code

Code: Select all


$connec = mysql_connect('****','****','****');
mysql_select_db('****', $connec);
$sql = "LOAD DATA LOCAL INFILE '*****.csv'
					INTO TABLE myworksheet 
					FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY ';' 
					
					LINES TERMINATED BY '\r\n'
					IGNORE 1 LINES
					";
					
mysql_query($sql);
to import file, but somewhere I have it ";" . How to import this different field.
This is my row from csv file.
[text]
401,1,colomn4,500053;example1;0000000585;21.07.2011,,,,"0,000","0,000","0,000","00,000,000","0,000","0,000","0,000",0,"0,000","0,000","0,000","0,908","0,000","0,000","0,000",0,"0,000","0,000","0,000",0,"0,000","0,000","0,000",0,"0,000","0,000","0,000",908,"0,000","0,000","0,000","00,000,000"
[/text]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Import csv file to MySQL with two different fields termi

Post by Christopher »

You should replace ";" with "," in the text file first -- then import. If you are on Unix the you could exec a command line program like sed from PHP.
(#10850)
krasi_e_d
Forum Newbie
Posts: 20
Joined: Mon Feb 27, 2012 5:00 am

Re: Import csv file to MySQL with two different fields termi

Post by krasi_e_d »

Christopher wrote:You should replace ";" with "," in the text file first -- then import. If you are on Unix the you could exec a command line program like sed from PHP.
How can I do automatically?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Import csv file to MySQL with two different fields termi

Post by Celauran »

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Import csv file to MySQL with two different fields termi

Post by Christopher »

You can execute sed from within PHP: http://us1.php.net/manual/en/book.exec.php
(#10850)
Post Reply