如何从 PHP 中的此 MySQL 查询响应中获取值
How to fetch value from this MySQL query response in PHP
以下是我的查询。我评论说我在代码中尝试过
$query = "SELECT SUM(paymentAmount) From salepayment WHERE adminUserId ='$adminUserId' AND invoiceId = '$salesInvoiceId' ";
$this->res = $this->db->prepare($query);
$this->result = $this->res->execute();
$countAmount = $this->res->rowCount();
if ($countAmount > 0) {
$sumOfPaymentAmount = $this->res->fetch(PDO::FETCH_OBJ);
echo json_encode($sumOfPaymentAmount);
// $totalPaid = $sumOfPaymentAmount->"SUM(paymentAmount)";
// echo json_encode($totalPaid);
}else{
$paid = 0;
}
这是我的回复
{
"SUM(paymentAmount)": "350"
}
我想将此值放入 $paid
变量中。
感谢您的帮助。我是 PHP 和服务器端编码的初学者,请原谅我的错误。
这应该可以通过在查询中的 SUM(paymentAmount)
之后添加 as paid
来完成,如下所示:
SELECT SUM(paymentAmount) as paid From...
以下是我的查询。我评论说我在代码中尝试过
$query = "SELECT SUM(paymentAmount) From salepayment WHERE adminUserId ='$adminUserId' AND invoiceId = '$salesInvoiceId' ";
$this->res = $this->db->prepare($query);
$this->result = $this->res->execute();
$countAmount = $this->res->rowCount();
if ($countAmount > 0) {
$sumOfPaymentAmount = $this->res->fetch(PDO::FETCH_OBJ);
echo json_encode($sumOfPaymentAmount);
// $totalPaid = $sumOfPaymentAmount->"SUM(paymentAmount)";
// echo json_encode($totalPaid);
}else{
$paid = 0;
}
这是我的回复
{
"SUM(paymentAmount)": "350"
}
我想将此值放入 $paid
变量中。
感谢您的帮助。我是 PHP 和服务器端编码的初学者,请原谅我的错误。
这应该可以通过在查询中的 SUM(paymentAmount)
之后添加 as paid
来完成,如下所示:
SELECT SUM(paymentAmount) as paid From...