Blog

Back 21 Comments

Tutorial: Import a CSV File Using PHP and MySQL


The following code should help beginner PHP/MySQL developers who are looking for an easy way to import a CSV or comma delimited file into a mysql database.  This example adds new contacts into the contacts table from an uploaded CSV file, populating the following three fields: (contact_first, contact_last, contact_email).

Click to view files:

I've included the PHP code below that 'does all the work'.  Click the 'import.php' link above to view the full code.

    //get the csv file
    $file = $_FILES[csv][tmp_name];
    $handle = fopen($file,"r");
   
    //loop through the csv file and insert into database
    do {
        if ($data[0]) {
            mysql_query("INSERT INTO contacts_tmp (contact_first, contact_last, contact_email) VALUES
                (
                    '".addslashes($data[0])."',
                    '".addslashes($data[1])."',
                    '".addslashes($data[2])."'
                )
            ");
        }
    } while ($data = fgetcsv($handle,1000,",","'"));
    //

rice
February 15, 2013 9:33am
thank you for sample
anil
September 19, 2012 2:57am
hi friend your code very nice but you use to fopen and i need without fopen so pls with out fopen import file phpmysql data base
himanshu
November 10, 2012 4:12am
it is not working properly
patrick
September 20, 2012 11:02am
thanks. works great.
Prove the code and it works!!!
Raul
October 10, 2012 3:16pm
Hey man i got an error, it says, $data is undefined, it works perfect in localhost, but in a production website throws this error.
ratan
October 17, 2012 2:22am
hi there,
i am having a problem here.. the ERR is

Notice: Use of undefined constant csv - assumed 'csv' in C:\xampp\htdocs\index.php on line 8

Notice: Use of undefined constant size - assumed 'size' in C:\xampp\htdocs\index.php on line 8

Notice: Undefined index: csv in C:\xampp\htdocs\index.php on line 8

can you please check it out...

Regards,
Ratan Thapa
+9779849086678
mail@ratan.com.np
www.ratan.com.np
Babulal Saini
October 18, 2012 1:38am
This is very nice.....
Clint
October 25, 2012 8:43am
hey great tutorial, works excellent. Though it took me a while to code the error checking and validation I needed, this got me off to a great start, as someone new to PHP and MySQL, I really appreciate this.
ishant
November 1, 2012 2:47pm
can u also provide the similar script for codeigniter?
dav
November 18, 2012 2:08pm
hello,can you tell my why i got the object not found error??many thx
Abc
November 21, 2012 1:15am
wrk fine..
Khalid
November 23, 2012 6:52am
works fine :)
afeeqah
December 8, 2012 8:55am
i have more than one table..so i want it can browse any table..so how i change this rows..
do {
if ($data[0]) {
mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));

the statement "INSERT INTO contacts (contact_first, contact_last, contact_email) mean only can select one table from contact right?
saurabh
December 14, 2012 3:31am
Thanks...
It's working fine at all.

Saurabh Kumar
(software developer)
Felipe
January 9, 2013 1:26pm
Awesome, that works! Thanks a lot.
Lelouch
January 10, 2013 2:19am
Can you also post importing .csv file using codeignite?please
JY
January 10, 2013 2:22am
Can you also post importing .csv file using codeigniter? this code works but i need to use implement it using codeigniter!please
sourabh kasliwal
January 31, 2013 12:48am
very nice simple csv read write code i have found on http://wininterview.blogspot.in/2013/01/read-and-write-csv-file-in-php.html
prathyusha
February 10, 2013 11:18pm
i use(import.php) code its working fine thank you
VIVEK KUMAR
February 26, 2013 2:08am
Working fine...
SNoNo
March 7, 2013 9:07am
Thank you for sharing

Shame on some of you with silly questions, with very simple code, if you cant debug, dont use more PHP, for your security !


Your comment has been posted....