Import excel data into php-mysql records

To import excel data into php-mysql records
  1. Create a table with required fields. 
  2. Make database connection. 
  3. Open CSV file and read data. 
  4. Create and run the sql query till the end of the file.
The code is given here. It works !!


  
  // Connect to your database
  mysql_connect('localhost','username','password');
  mysql_select_db('dbname');

  // Open the file for reading
  $file = "sample.csv";
  $handle = fopen($file,"r");

 do {
 if($data[0]){
 $q="INSERT INTO products (name, brand, price) VALUES
  (
   '".mysql_real_escape_string($data[0])."',
   '".mysql_real_escape_string($data[1])."',
   '".mysql_real_escape_string($data[2])."'   
  )";
 mysql_query($q);
 }
 } while ($data = fgetcsv($handle, 1000, ","));

Import excel data into php-mysql records Import excel data into php-mysql records Reviewed by JS Pixels on December 30, 2010 Rating: 5

4 comments:

  1. how to send the more than 1000 mail from excel sheet sheet at a time as click on send mail button in php ?

    please help me ........


    santoshverma212@gmail.com

    ReplyDelete
  2. Download excel sheet reader from
    http://sourceforge.net/projects/phpexcelreader/
    and after reading records use mail() function inside loop. This will surely help you.

    ReplyDelete
  3. Altaf hussain, good works.. :P

    ReplyDelete

Altaf Web. Powered by Blogger.