I have a CSV file I loop thru to get an array od data that looks like this:
$data[0]['StockNumber']
This array as 400 different rows. I need to compare these values against my database and do 3 action.
Select data from my database and return as an object.
If $data[0]['stockNumber'] exist in my DB, I want to update my database. If $data[0]['stockNumber'] doesn't exist, I want to add to my database.
After that, if a stock number exist in my DB but not in my CSV, I want to delete from my DB.
How can I loop this?
here is the code I am using
function ImportCSV2Array($filename)
{
$row = 0;
$col = 0;
$handle = @fopen($filename, "r");
if ($handle)
{
while (($row = fgetcsv($handle, 4096,'|','"')) !== false)
{
if (empty($fields))
{
$fields = $row;
continue;
}
foreach ($row as $k=>$value)
{
$results[$col][$fields[$k]] = $value;
}
$col++;
unset($row);
}
if (!feof($handle))
{
echo "Error: unexpected fgets() failn";
}
fclose($handle);
}
return $results;
}
$csvArray = ImportCSV2Array($filename);
print_r($csvArray[4]['StockNumber']);
"SELECT StockNumber FROM my_table"
$result = $conn->query($sql);
Now, how can I check all the valus from $result against value in $results