excel data unable to insert in MySql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpfan
Forum Newbie
Posts: 14
Joined: Thu Aug 27, 2009 4:37 am

excel data unable to insert in MySql

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: excel data unable to insert in MySql

Post 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
phpfan
Forum Newbie
Posts: 14
Joined: Thu Aug 27, 2009 4:37 am

Re: excel data unable to insert in MySql

Post by phpfan »

thanks for the reply
Post Reply