?
Solved

Import csv into Mysql with PHP Script

Posted on 2004-11-28
9
Medium Priority
?
1,661 Views
Last Modified: 2013-12-12
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.
0
Comment
Question by:edgardoburgos
6 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 672 total points
ID: 12693935
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
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 664 total points
ID: 12696858
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
 

Author Comment

by:edgardoburgos
ID: 12705813
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:nitinmehta
ID: 12790280
$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
 
LVL 4

Assisted Solution

by:nitinmehta
nitinmehta earned 664 total points
ID: 12790347
$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
 
LVL 48

Expert Comment

by:hernst42
ID: 13028242
   Split: hernst42 {http:#12693935} & snoyes_jw {http:#12696858} & nitinmehta {http:#12790347}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question