Loading table data from a CSV file : CSV « Development « PHP






Loading table data from a CSV file


<?php

   $conn = mysql_connect("localhost","someuser","secret");
   $db = mysql_select_db("cars");

   $fh = fopen("yourCSVFile.csv", "r");
   while ($line = fgetcsv($fh, 1000, ",")) {
      $year = $line[0];
      $make = $line[1];
      $color = $line[2];

      $query = "INSERT INTO carcollection SET year='$year', make='$make',color='$color'";
      $result = mysql_query($query);
   }

   fclose($fh);
   mysql_close();
?>

           
       








Related examples in the same category

1.explode CSV file
2.fgetcsv: read a csv file
3.Read csv file