Doing, Learning, and Sharing

Try to be The Best

Dec
17
2009

PHP read CSV File and insert to Mysql DB

Simple code for read CSV File and insert into mysql database per rows.

following this code:

PHP:
  1. mysql_connect("localhost","root","admin");
  2.      mysql_select_db("contact");
  3.      $filename="data.csv";
  4.      $handle = fopen("$filename", "r");
  5.      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
  6.      {
  7.        $import="INSERT into all_number(Name,Adress,Code) values('$data[0]','$data[1]','$data[2]')";
  8.        mysql_query($import) or die(mysql_error());
  9.       }
  10.      }
  11.      fclose($handle);
  12.    mysql_close();

I hope, This is helpfull for all

Powered by WordPress