Skip to content
Advertisement

How to fetch value from this MySQL query response in PHP

Below is my query. I have commented that I tried in code

$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;
                    }

Here is my response

{
    "SUM(paymentAmount)": "350"
}

I want to get this value into the $paid variable.

Thanks for your help. I am a beginner at PHP and server-side coding so pardon me for my mistakes.

Advertisement

Answer

This should be able to be done by adding as paid after SUM(paymentAmount) in the query like so:

SELECT SUM(paymentAmount) as paid From...
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement