Thursday, July 21, 2011

Import CSV Data into Mysql

Here We are converting CSV data into Mysql.
example: we have name,email,phone now we are importing .csv data into mysql
below is the code for this
<?
   if(isset($_POST['submit']))
    {

         $fname = $_FILES['sel_file']['name'];
     
         $chk_ext = explode(".",$fname);
     
         if(strtolower($chk_ext[1]) == "csv")
         {
     
             $filename = $_FILES['sel_file']['tmp_name'];
             $handle = fopen($filename, "r");
     
             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
             {
                $sql = "INSERT into user(name,email,phone) values('$data[0]','$data[1]','$data[2]')";
echo $sql;
               // mysql_query($sql) or die(mysql_error());
             }exit;
     
             fclose($handle);
             echo "Successfully Imported";
         }
         else
         {
             echo "Invalid File";
         }
    }
?>
    <form action='' method='post' enctype="multipart/form-data">

        Import File : <input type='file' name='sel_file' size='20'>
        <input type='submit' name='submit' value='submit'>

    </form>
OUTPUT:


Import File :


No comments: