By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,643 Members | 1,797 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,643 IT Pros & Developers. It's quick & easy.

Import database from excel sheet to PHP

P: 1
Can any one help in importing database from excel sheet to PHP?
Bye,
Sound.
Jan 19 '07 #1
Share this Question
Share on Google+
3 Replies


P: 33
Hi,

If your excel file is in CSV format you can do the following

1. In PHP you can use fgetcsv command to process and try insert into mysql on help of mysql commands

2.The best method is try using the following mysql statement

[PHP]
LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3);
[/PHP]


Subash :)
Jan 21 '07 #2

ronverdonk
Expert 2.5K+
P: 4,258
Can any one help in importing database from excel sheet to PHP?
Bye,
Sound.
Do you want to import a database FROM an Excel sheet INTO PHP? Obviously not. So it is either:

- import an Excel sheet (not all cells) into a MySQL database table using PHP
- import an Excel sheet (all cells) into a MySQL database table using LOADFILE
- export a MySQL table into an Excel XML sheet using PHP.

Which one is it?

Ronald :cool:
Jan 21 '07 #3

P: 2
If you want to import an exel sheet with php to your database, you must have sheet by sheet save as csv and then you can use this ex code


[PHP]<?php
//reading csv file
$fname="file.csv";
$fp=fopen($fname,"r") or die("csv Error");
$line = fgets( $fp, 2024 );
//conexion with db started
$c=mysql_connect("localhost","root","");
if(!$c)
{
die("Not Conected");
}
if(!mysql_select_db("dbspl"))
{
die("Table Error");
}

while(!feof($fp))
{
list($var1,$var2,$var3) =split( ",", $line); //list of variables to import
$line = fgets( $fp, 2024 );
$ctd=$ctd+1;
if($ctd > 1)
{
$var1=strtoupper($var1);//Uppercase

if($loca!="" && $pudoc!="")
{
if(!($inserc=mysql_query("INSERT INTO table (field1,field2,field3)
VALUES('$var1','$var2','$var3')")))
{
echo'<center>';
echo'<br>';
echo mysql_error();
echo'<br>';
echo"User Inser Error";
echo'</center>';
}
}//
}// if($ctd > 1)
} // while(!feof($fp))
fclose($fp);
?> [/PHP]
Feb 7 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.