PHP - Get sum of SQL through Prepared Statment

0

Good,

             $stmt2 = $con->prepare("SELECT SUM(product_qty) AS value FROM public_order_checklist WHERE order_code = '$order_code'");
             $stmt2->execute();
             $stmt2->bind_result($value);
             $stmt2->store_result();                
             $rowsx = $stmt2->fetch();

I would like to get the result of the total of all the "order_code" codes, But .. error appears:

Notice: Trying to get property of non-object in D:\xampp\htdocs\Superfacil_v1.4.6\inc\colaborator\delivery.php on line 374

Linha:  $rows = $stmt->fetch();
    
asked by anonymous 18.11.2018 / 22:54

1 answer

0

Hello, I noticed that you are using PDO, right? In this case you could try this:

$stmt2 = $con->prepare("SELECT SUM(product_qty) AS value FROM public_order_checklist WHERE order_code = :orderCode");
$stmt2->bindValue(":orderCode", $order_code);
$stmt2->execute();
$soma = $stmt2->fetchColumn();

Where $order_code is the code you want to use in the WHERE and $soma clause contains the value you expect.

    
19.11.2018 / 03:08