Sunday, August 11, 2013

CSV

Comma Separated Value file

Create the file in excel

put values in quotes and separated with commas.


<?php
//Setup the constants
DEFINE ('DBUSER', 'username');
DEFINE ('DBPW''password');
DEFINE ('DBHOST''localhost');
DEFINE ('DBNAME''customer');

if ($dbc = mysql_connect(DBHOST, DBUSER, DBPW)) 
   {
      if (!mysql_select_db(DBNAME))
         {
            trigger_error("Couldn't select database<br>MySQL Error: " . mysql_error());
            exit();
         }
   } else {
   trigger_error("Couldn't select database<br>MySQL Error: " . mysql_error());
}

$file = fopen("myfile.csv", "r");

while(($info = fgetcsv($file, 1000, ',')) != FALSE)
{
   $getdata = implode("," $info);
   $query = "INSERT INTO manufacturer (man_id,manifacturer_name) VALUES (' . $getdata . ')";
   echo $query . "<br>";

   $result = mysql_query($query) or trigger_error("Query<br>MySQL Error" .  mysql_error());
}

mysql_close();
?>

No comments:

Post a Comment