Link to home
Start Free TrialLog in
Avatar of edgardoburgos
edgardoburgos

asked on

Import csv into Mysql with PHP Script

I urgently need a script that would read a csv file that i exported from filemaker pro and import it into Mysql using php. I search the web for days and found a few scripts but none of them worked. Thank you ahead of time for your time.
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edgardoburgos
edgardoburgos

ASKER

How do I put this code together so that it works? The links/code above have different snippents of code. can i just copy and paste it, and save it as a php file and it will work. Of course modify it to work with my server and cvs file.
$link = mysql_connect("localhost", "username", "password") or die("Could not connect: ".mysql_error());

$db = mysql_select_db("db_name") or die(mysql_error());

$query = "LOAD DATA INFILE './data_file.csv' INTO TABLE table_name(`field1`, `field2`,.....)";
$result = mysql_query($query)
   or die("Invalid query: " . mysql_error());

fclose ($handle);

If number of fields in table match the number of data columns in file, you can skip the field list part, i.e.

$query = "LOAD DATA INFILE './data_file.csv' INTO TABLE table_name";

REMEMBER : the data file should be in the same directory as mysql table.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
   Split: hernst42 {http:#12693935} & snoyes_jw {http:#12696858} & nitinmehta {http:#12790347}