Tuesday 1 January 2013

Insert data in to table from excel file in php


Step -1 :

Open excel file and save as "TAB DELIMITED" text file.
in below sample code, "example.txt" is tab-delimited excel file.

Step -2 : 

Now, follow the below instruction for create code for insert data into table in mysql using php.


<?php

include 'config.php';
// Connect to your database

$fp = fopen("example.txt", "r");    // Open the file('Tab Delimited') for reading

while($line = fgets($fp))      // Loop through each line
{
 
     list ($name, $first) = explode("\t", $line);

     // Split the line by the tab delimiter and store it in our list...

     $sql = "insert into user (name,phone) values ('$name', '$first')";

     // Generate sql string...

     mysql_query($sql) or die ( mysql_error() );        // Execute the sql

}

?>

No comments:

Post a Comment