Tag Archives: fixForDB

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);

}

}

}

 

?>

custom db query, and db prep functions

20 Sep

here’s a few functions that are required by some of my code / functions.

make_db_connection

function make_db_connection($conn_name, $conn_sql)
{

global $site_access, $database_site_access;

$covert_this_str = "global \$" . $conn_name . " ;";
eval($covert_this_str);
$covert_this_str = "\$" . $conn_name . "= mysql_query(\$conn_sql,\$site_access ) or die(mysql_error());";//report_error(‘002′,’" . addslashes($conn_sql) . "’));";
//echo $conn_sql."<br />";
eval($covert_this_str);
// addKill($conn_name);
}

fixForDB

function fixForDB($item,$nonEmpty=0){

$item=trim($item);
if(empty($item)){

$item=$nonEmpty;

}

if(isNaN($item)){
$item=”‘”.addslashes($item).”‘”;
}

return $item;
}

isNaN
this isn’t a function of mine. I found it on the php.net manual site. if you wrote it, please let me know so i can give proper credit

function isNaN( $var ) {
return !ereg (“^[-]?[0-9]+([\.][0-9]+)?$”, $var);
}