php csv to mysql importing script
20 Sep
Here’s a function to import csv data into mysql. It assumes the first line of your csv file contains the table column names.
also it uses custom functions make_db_connection and fixForDB either use the functions or write your own. get them here:
http://elsid.net/2007/09/20/custom-db-query-and-db-prep-functions/
<?php
function processCsv($csvData,$seperator,$lineSeperator,$table){
global $findCurrentListing;
//process csv data
/*data should be in the following format
@line 1 = column names seperated by $seperator
all following lines should be data separated by $seperator with data groups separated by $lineSeperator
*/
//data shouldn’t have quotations around values, so lets remove them.
//also we’ll add slashes to data before inserting
//i’m using 0 as a subtitution for empty values. 0 will work for int,boolean, char values so thats why i choose it’
$nonEmpty=0;$csvData=str_replace(""","",$csvData);
$csvContent=explode($lineSeperator,$csvData);
//get rid of last item, it contains empty values
array_pop($csvContent);
$csvContent[0]=str_replace($seperator,’,',$csvContent[0]);$csvColumns=explode(‘,’,$csvContent[0]);
$csvTotal=count($csvContent);
$csvColumnsNum=count($csvColumns);for($loop=1;$loop<$csvTotal;$loop++){
set_time_limit(5);
$currData=explode($seperator,$csvContent[$loop]);
$fieldList=str_replace(",","`,`",$csvContent[0]);
$csv2DbInsertSQL="INSERT INTO $table ( `".trim(str_replace(" ","",$fieldList))."` ) VALUES (";
$csv2DbUpdateSQL="UPDATE $table SET ";
$csv2DbFindSQL="SELECT * FROM $table WHERE ";for($buildLoop=0;$buildLoop<$csvColumnsNum;$buildLoop++){
$csv2DbInsertSQL.=" ".fixForDB($currData[$buildLoop],$nonEmpty).",";
$csv2DbUpdateSQL.=" `".trim($csvColumns[$buildLoop])."`=".fixForDB($currData[$buildLoop],$nonEmpty).",";}
$csv2DbInsertSQL=substr($csv2DbInsertSQL,0,strlen($csv2DbInsertSQL)-1).")";
$csv2DbUpdateSQL=substr($csv2DbUpdateSQL,0,strlen($csv2DbUpdateSQL)-1);
$csv2DbFindSQL.=" `".trim($csvColumns[0])."`=".fixForDB($currData[0],$nonEmpty);
$csv2DbUpdateSQL.=" WHERE `".trim($csvColumns[0])."`=".fixForDB($currData[0],$nonEmpty);
make_db_connection("findCurrentListing",$csv2DbFindSQL);
if(mysql_num_rows($findCurrentListing)>0){
make_db_connection("processItem",$csv2DbUpdateSQL);}else{
make_db_connection("processItem",$csv2DbInsertSQL);}
}
}
?>


it’s good idea for to cross convert database in the future, thanks ….