Saturday, 7 September 2013

CSV Importer in MySQL

CSV importer is a PHP file which can help you to import all your data in MySQL Database using CSV File. Source File csv_importer.php is also available with this tutorial.



Source File link is Here

  1. Open your Dreamweaver or Notepad editor and create new PHP Page and name it import.php
  2. Now include csv_importer.php file in your page like below.
    • include ( "csv_importer.php" );
  3. For Connecting MySQL Database please write following line
    • $conn = @mysql_connect("localhost","root","password");
  4. Now choose your database like below
    • @mysql_select_db("yourdbname",$conn);
  5. Now write below lines . you just need to provide your table name where you want to import data. leave all other lines as it is.
    • create new importer object for importing data
      $c = new CSV_Importer;
      display log errors at end           
      $c->log_errors = true;
      skip the very first row in CSV file         
      $c->skip_top   = true;
      Type of Server (default MYSQL), you can also use this  MSSQL and PGSQL          
      $c->server     = MYSQL; 
      Database Table where File will be imported         
      $c->table      = "yourtablename";
  6. Now you have to set columns of table according to your CSV file Template
    • $c->SetColumnSequence("Field1,Field2,Field3,Field4,Field5");
  7. Here you will write csv file reference from which your data will be imported to Table
    • $result = $c->import("Your_CSV_Name.csv",$conn);
  8. Now include csv_importer.php file in your page like below.
    • if($result === FALSE)
      {
      there was some error importing data
      $c->DumpErrors();
      }
      else
      {
      Your data imported successfully, it will print number of rows inserted.
      print "Total records inserted are $result in table $c->table";
      } 
  9. in the end you will close MySQL Connection
    • @mysql_close();
Above tutorial is based on scriptbaba 2009 scripts. Now that website is not working so I write this tutorial for you. For more information please open  Crewow Website