3 hours ago, djinni said:Hi all,
I can't seem to get this working, what am I doing wrong. I'm, on 1.7 and gettnig this in my error log:
PHP Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /public_html/updater.php on line 34
Line 34: while($row = mysqli_fetch_assoc($query)){and
PHP Notice: Undefined offset: 4 in /public_html/updater.php on line 45
Line 45: if(!empty($csv[$x]['Reference']) && !empty($csv[$x]['Quantity']) || $csv[$x]['Quantity'] !==0 ){
Thanks
Try this. Switched to PDO, also what query do you use to get stock from ypour shop. Here you update only products with combinations. If you want to do simple products and products with combinations you need to modify it.
<?php set_time_limit(900); #version 2 $database = require_once('config/configup.php'); // Create connection function connect(){ global $database; try{ $db = new PDO('mysql:host='.$database['database_host'].';dbname='.$database['database_name'], $database['database_user'], $database['database_password']); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo 'connected!.<br />'; }catch(PDOException $e){ echo 'connection failed.<br />'; } return $db; } Function shop_products(){ $db = connect(); $array=array(); $query= $db->prepare('select ps_product.id_manufacturer AS manuid,ps_product_attribute.reference AS REF,ps_stock_available.quantity AS QTY from ps_product LEFT JOIN ps_product_attribute on ps_product_attribute.id_product=ps_product.id_product LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute'); try{ $query->execute(); while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) { if(!empty($row['REF']) && $row['manuid'] == 6){ $array[]=$row; } } echo 'array filled.<br />'; }catch(PDOException $e){ echo 'array error.<br />'; } return $array; } Function readCSV($csvFile){ $array = $fields = array(); $i = 0; $handle = @fopen( $csvFile, "r"); if ($handle) { while (($row = fgetcsv($handle, 4096, ",")) !== false) { if (empty($fields)) { $fields = $row; continue; } foreach ($row as $k=>$value) { $array[$i][$fields[$k]] = $value; } $i++; } if (!feof($handle)) { echo "Error: unexpected fgets() fail\n"; } fclose($handle); } return $array; } Function Update(){ $db = connect(); $csvFile = 'path to csv file'; $csv = readCSV($csvFile); $sp = shop_products(); $query = $db->prepare("UPDATE ps_product_attribute LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute LEFT JOIN ps_product on ps_product_attribute.id_product=ps_product.id_product SET ps_stock_available.quantity = '".$avbl."' WHERE ps_product_attribute.reference = '".$indeks."'"); foreach($sp as $s){ $indeks = $s['REF']; $qty = $s['QTY']; foreach($csv as $stock){ $index = $stock['Reference']; $avbl = $stock['Quantity']; if($indeks == $index){ if($qty != $avbl){ try{ $query->execute(); $log = $indeks.PHP_EOL. "quantity changed from ".$qty." to ".$avbl.PHP_EOL. "-------------------------".PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); }catch(PDOException $e){ $log = $indeks.PHP_EOL. "error ".$e->getMessage().PHP_EOL. "-------------------------".PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); die(); } } } } } } Function oldfile(){ $files = glob('./log/*.log'); $now = time(); foreach ($files as $file) { if (is_file($file)) { if ($now - filemtime($file) >= 60 * 60 * 24 * 3) { // 2 days unlink($file); } } } } $log = "⇊ Update Starts ⇊ ".date("H:i:s").PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); Update(); $log = "⇈ Update Finished ⇈ ".date("H:i:s").PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); oldfile(); unset($database); exit("exit"); ?>