Hi all,

I want to import a .csv file into mysql database with using php code.
How i can do this . what php code i will have to use for this. Please help me to do this. Because i have thousand of records which is in .csv file format. If i will put it manully in my databae . It will take so much time. If there is any code to import directly. Please provide me or help me to solve this problem.

With Regards,

Member Avatar


maybe the tool [snipped - no advertising] solve your problem.
Excel/CSV => PHP => mySQL



Looks like a cool app Andy, BUT you fail to mention that users have to pay you for the privilege. Shame really.


you are right. I really feel ashamed. :-)

In the past (3 years) I did have a DONATE button for snipped and
snipped. Unfortunately I had received only 5 Euro from 2 friends.

...so - I have changed my model.

Sorry for that


The code realized with fgetcsv() should be something like this :

PHP Code:
$fp         = fopen("english.csv", "r");

$length         = 4096; /// have to be optional length ...
$fildDelineate  = ','; /// or "|" ... declare what you need
$databasetable  = 'tableName';
$counter        = 1; // to omission first row if it is table headers

while( !feof($fp) ) {

  if( !$line = fgetcsv($fp, $length, $fildDelineate, '') && $counter) {
  foreach($line as $key => $value){
   // For example insert in 3 column (e.g. id, name, email )
   $importSQL = "insert into $databasetable values('$line[0]','$line[1]','$line[2]')";
   mysql_query($importSQL) or die(mysql_error());