PHP - Getting SQL sum through Prepared Statment

Asked

Viewed 20 times

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 "order_code", But.. error appears error:

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();

1 answer

0

Hello, I noticed you’re using PDO, right? In that case you could try it that way:

$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 clause WHERE and $soma contains the value you expect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.