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.
edgardoburgosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
A simple script would look like this (from http://de3.php.net/fgetcsv):

$link = mysql_connect("localhost", "username", "password") or die("Could not connect: ".mysql_error());
// username = user account name
// password = password to user acc
$db = mysql_select_db("ip2country") or die(mysql_error());
// ip2country table is define on top
$row = 1;
// adopt the filename
$handle = fopen ("ip-to-country.csv","r");
// ip-to-country.csv must be in the same directory with this php file
while ($data = fgetcsv ($handle, 1000, ",")) {
    // adopt the columnnames
   $query = "INSERT INTO ip2country(`ipFrom`, `ipTo`, `country2`, `country3`, `country`) VALUES('".
       // data[0] will be the first column in your csv, ...
       .$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."')";
   $result = mysql_query($query) or die("Invalid query: " . mysql_error().__LINE__.__FILE__);
}
fclose ($handle);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snoyes_jwCommented:
If it's a big file, you might look into using LOAD DATA INFILE instead of creating lots of INSERTs.  It can be 20 times faster.

http://dev.mysql.com/doc/mysql/en/LOAD_DATA.html
http://dev.mysql.com/doc/mysql/en/Insert_speed.html
0
edgardoburgosAuthor Commented:
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.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

nitinmehtaCommented:
$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.
0
nitinmehtaCommented:
$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 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";

REMEMBER : the data file should be in the same directory as mysql table.
0
hernst42Commented:
   Split: hernst42 {http:#12693935} & snoyes_jw {http:#12696858} & nitinmehta {http:#12790347}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.