Simple php script for transfering excel data (convert to .csv file first ) to finally to mysql.

685 views
Skip to first unread message

zeesh...@gmail.com

unread,
Feb 18, 2006, 2:24:37 AM2/18/06
to phpguru
Simple php script for transfering excel data (convert to .csv file
first ) to finally to mysql.

example.php

<?
// Developed by Zeeshan Saturday February 18, 2006

set_time_limit(0);

$db_host = "localhost";$db_user = "abc";$db_pass = "123";$db_name =
"abc";
$db_conn =
mysql_connect($db_host,$db_user,$db_pass);mysql_select_db($db_name);
$filename = "mycsv.csv";

$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
$contents1 = explode("\n", $contents);
$total = count($contents1);

for( $i=0; $i< count($contents1); $i++ )
{
$cell = explode(";", $contents1[$i]);
$fldstr = "";$sep = ", ";

for($j=0;$j<count($cell)-1;$j++){
$fldstr .= "'".$cell[$j]."'".$sep;
if($j == 24) $sep = ""; else $sep = ", ";
}
$fldstr = str_replace("\"", "", $fldstr);
//echo $fldstr."<hr>";
$strQry = "INSERT INTO tbl_personal_data ( col1, col2 ...col26 )
VALUES(".$fldstr.");";
//echo $strQry."<br><br>";if($i==5) die();
mysql_query($strQry,$db_conn);
echo $i.", ";
}
fclose($handle);
echo "<script>alert('$total records are successfully
inserted');</script>";
?>


If you want to transfer data directly from excel to mysql then download
codes from http://sourceforge.net/projects/phpexcelreader/
OR Search in google as "Spreadsheet_Excel_Reader"

Thanks

Regards
Zeeshan

Reply all
Reply to author
Forward
0 new messages