To import excel data into php-mysql records
- Create a table with required fields.
- Make database connection.
- Open CSV file and read data.
- Create and run the sql query till the end of the file.
// 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,",","'"));
Posting Komentar