Page 1 of 1

excel data unable to insert in MySql

Posted: Thu Sep 17, 2009 2:04 am
by phpfan

Code: Select all

<?php
$db_username="root"; //database user name
$db_password="";//database password
$db_database="example"; //database name
$db_host="localhost";
 
 
mysql_connect($db_host,$db_username,$db_password);
@mysql_select_db($db_database) or die( "Unable to connect to database.");
 
 
$handle = fopen("SMS.xls", "r"); //test.xls excel file name
if ($handle)
{
$array = explode("\n", fread($handle, filesize("SMS.xls")));
}
 
$total_array = count($array);
$i = 0;
 
while($i < $total_array)
{
$data = explode(",", $array[$i]);
$sql = "insert into excel values ('$data[0]')"; // i have two fields only , in oyour case depends on your table structure
$result = mysql_query($sql);
 
$i++;
}
echo "completed";
?>
 
Hi, when im running the script the im getting some data with symbols,the data is inserting but im not getting the correct that that i need
kindly help me on this

Re: excel data unable to insert in MySql

Posted: Thu Sep 17, 2009 4:57 am
by Mark Baker
You're not reading an Excel file. You're reading a CSV file with an extension of .xls.
So use PHP's built in fgetcsv() function.

Then look at the character set used for your mySQL tables and for the file that you're getting

Re: excel data unable to insert in MySql

Posted: Thu Sep 17, 2009 5:14 am
by phpfan
thanks for the reply