Home » , » Import excel data into php-mysql records

Import excel data into php-mysql records

Written By Unknown on Rabu, 29 Desember 2010 | 23.29

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,",","'"));

Share this article :

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger