if(file_exists($file)){
echo "File Exists...Opening $file\n";
$f = fopen("$file","r");
} else {
die("There was an error opening file $file");
}
$insert = "INSERT INTO $tableName (";
$columns = "";
$values = "";
$count = 1;
$params = array(2, 709);
$rows_affected = 0;
while($array = fgetcsv($f,0,"\t")){
$values = "";
foreach ($array as $value) {
if($count == 1) {
$columns .= "$value, ";
/*if($value == "ListingNumber") {
$listingNumberIndex = key($array) - 1;
echo $listingNumberIndex;
}*/
} else {
$value = str_replace(''', '\'\'', $value);
$value = str_replace('&', '&', $value);
$values .= "'$value', ";
}
}
if($count == 1) {
$columns = substr_replace($columns,"",-2);
$count = 2;
} else {
$sql = $insert . $columns . ") VALUES (" . substr_replace($values,"",-2) . ")";
echo "CURRENT MEMRY USAGE = " . memory_get_usage()/1048567 . "\n";
// PRINT CURRENT MEMRY LIMIT:
sqlsrv_query( $conn, $sql,$params);
} //END ELSE COUNT ==2
} //END WHILE GET NEXT LINE
I know I can change the order the fields are in the insert but I would then have to hardcode it to a particular order, which I don't necessarily want to do, because columns can be added and the order can change in the future. The only thing I know about the files contents is that it will always have all of the columns that are fed from the RETS server which should match all of the columns in my database but not necessarily the order. I also don't want to hardcode them because there are about 10 tables that all have 200+ columns.
MH