I am having trouble with this cron running a php script. It should deactivate all products for both tables and then compare the csv with whats in the tables and reactivate if the product is in the CSV (along with any changes to the product in the csv.)
The script works fine (albeit slow) if i run it in a browser. But on a cron it seems to deactivate all the products and nothing else. I am having trouble trying to find the solution here.
Edit: Forgot to mention that each CSV has around 50,000 entries.
Please let me know if you need any additional information such as the CSV structure.
The Cron PHP Script
<?php
set_time_limit(0);
//Deactivate All
mysqli_query($conn, "UPDATE products SET ACTIVE = 'false'");
mysqli_query($conn, "UPDATE CProducts SET ACTIVE = 'false'");
//Add or Update Products
$file = "temp/webprod.csv";
$csv = file_get_contents($file);
$Products = array_map("str_getcsv", explode("\n", $csv));
array_pop($Products);
array_shift($Products);
foreach ($Products as $item){
$productsSQL = mysqli_query($conn, "SELECT * FROM products WHERE REFERENCE = '".$item[0]."' LIMIT 1");
if (mysqli_num_rows($productsSQL) > 0){
//Update It or Activate
mysqli_query($conn, "UPDATE products SET PRODUCT_DESCRIPTION = '".$item[1]."', TILE_SIZE = '".$item[2]."', RETAIL_PRICE = '".$item[3]."', COST_PRICE = '".$item[4]."', ACTIVE = 'true' WHERE REFERENCE = '".$item[0]."'");
}else{
//Add It
mysqli_query($conn, "INSERT INTO products (REFERENCE, PRODUCT_DESCRIPTION, TILE_SIZE, RETAIL_PRICE, COST_PRICE) VALUES ('".$item[0]."', '".$item[1]."', '".$item[2]."', '".$item[3]."', '".$item[4]."')");
}
}
//Add or Update CProducts
$file = "temp/cwebprod.csv";
$csv = file_get_contents($file);
$CProducts = array_map("str_getcsv", explode("\n", $csv));
array_pop($CProducts);
array_shift($CProducts);
foreach ($CProducts as $c1Item){
$productsSQL = mysqli_query($conn, "SELECT * FROM CProducts WHERE REFERENCE = '".$c1Item[0]."' LIMIT 1");
if (mysqli_num_rows($productsSQL) > 0){
//Update It or Activate
mysqli_query($conn, "UPDATE CProducts SET PRODUCT_DESCRIPTION = '".$c1Item[1]."', TILE_SIZE = '".$c1Item[2]."', RETAIL_PRICE = '".$c1Item[3]."', COST_PRICE = '".$c1Item[4]."', ACTIVE = 'true' WHERE REFERENCE = '".$c1Item[0]."'");
}else{
//Add It
mysqli_query($conn, "INSERT INTO CProducts (REFERENCE, PRODUCT_DESCRIPTION, TILE_SIZE, RETAIL_PRICE, COST_PRICE) VALUES ('".$c1Item[0]."', '".$c1Item[1]."', '".$c1Item[2]."', '".$c1Item[3]."', '".$c1Item[4]."')");
}
}
?>