PhP
// $numberOption = the number of fields in TableA.
// $csvHeaders = a single dimention array with all the names of the fields in a CSV file.
// $tableHeaders = a single dimention array with all the names of the fields in TableA.
for ($a=0; $a < $numberOption; $a++) {
if ($a == 0) {
$toVars .= "@var$a";
$setCols .= $tableHeaders[$a] . " = @var" . $csvHeaders[$a];
} else {
$toVars .= ", @var$a";
$setCols .= ", " . $tableHeaders[$a] . " = @var" . $csvHeaders[$a];
}
}
$sql = "LOAD DATA LOCAL INFILE '".addslashes($current_file)."' REPLACE INTO TABLE $current_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '" . '"' . "' ESCAPED BY " . "'\\\\'" . " LINES TERMINATED BY '\n' IGNORE 1 LINES ($toVars) SET $setCols";
The returned value of $sql
"LOAD DATA LOCAL INFILE '\/var\/www\/html\/test_lms\/include\/files\/1349237011-2fb46cd0c360464ebf471755cc9580de-AUTO_INSURANCE.csv' REPLACE INTO TABLE auto FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\n' IGNORE 1 LINES (@var0, @var1, @var2, @var3, @var4, @var5, @var6, @var7, @var8, @var9, @var10, @var11, @var12, @var13, @var14, @var15, @var16, @var17, @var18, @var19, @var20, @var21, @var22, @var23, @var24, @var25, @var26, @var27, @var28, @var29) SET callcenter = @var, agent = @varEMPTY, generation_date = @varEMPTY, first_name = @varEMPTY, last_name = @varEMPTY, email = @var3, phone = @var4, address = @var5, city = @var6, state = @var7, dob = @varEMPTY, gender = @varEMPTY, marital_status = @varEMPTY, rented = @varEMPTY, year = @varEMPTY, make = @varEMPTY, model = @varEMPTY, trim = @varEMPTY, vin = @varEMPTY, primary_use = @varEMPTY, miles_oneway = @varEMPTY, mileage = @varEMPTY, license_num = @varEMPTY, license_state = @varEMPTY, education = @varEMPTY, job_title = @varEMPTY, vendors = @var9, license_status = @varEMPTY, zip = @var8, dupe = @varEMPTY
Okay clearly in the php line that says $setCols .= $tableHeaders[$a] . " = @var" . $csvHeaders[$a];
it is initializing $setCols
with a field name from tableA
and then the equal sign, then this thing @var
(which I guess is a mysql variable?), then a csv field name. But the result shows a number attached to the @var
sign and not a csv field name! Some @var
have EMPTY
attached to them and that just means there was no field name from the $csvHeaders
array to use so EMPTY
was returned.
Anyway, it works. I am able to load in a csv file just fine and perfectly actually. But I dont understand how it is able to SET
with @var#
and not @varCSVNAME
. Another question is why use @var
at all? Wouldn't IGNORE LINES 1 (table field names) SET (csv header names)
work just fine? Why the @var
's?