简体   繁体   中英

return statement doesn't return anything

So basically, I have a function getPayments(). This function should execute a query, selecting from multiple tables (with joined). Here is my code

function getPayments($userid, $schoolyear) {
    $stmt = $this->con->prepare("SELECT tbl_payment.payment_receipt_type AS RType, tbl_payment.payment_receipt_number AS RNumber, tbl_feetype.feetype_name AS FName, tbl_payment.payment_amount AS PAmount, tbl_month.month_date AS MDate, tbl_payment.payment_dateadded AS PAdded 
FROM tbl_payment
  INNER JOIN tbl_student ON tbl_student.student_id = tbl_payment.student_id 
  INNER JOIN tbl_schoolyear ON tbl_schoolyear.schoolyear_id = tbl_payment.schoolyear_id
  INNER JOIN tbl_feetype ON tbl_feetype.feetype_id = tbl_payment.feetype_id 
  INNER JOIN tbl_month ON tbl_month.month_id = tbl_payment.month_id   
  WHERE tbl_payment.schoolyear_id = ? AND tbl_payment.student_id = ? ORDER BY payment_dateadded DESC");

    $stmt->bind_param("ss", $userid, $schoolyear);
    $stmt->execute();
    $stmt->bind_result($RType, $RNumber, $FName, $PAmount, $MDate, $PAdded);

    $payments = array();

    while ($stmt->fetch()) {
        $temp = array();

        $temp['paymenttype'] = $RType;
        $temp['receiptnumber'] = $RNumber;
        $temp['feename'] = $FName;
        $temp['paymentamount'] = $PAmount;
        $temp['monthname'] = $MDate;
        $temp['paymentdate'] = $PAdded;

        array_push($payments, $temp);
    }

    return $payments;
}

In my index.php file:

//getting payment details for a user
$app->get('/payment/{id}/{sy}', function (Request $request, Response $response) {
$route = $request->getAttribute('route');
// $userid = $request->getAttribute('id');
$userid = $route->getArgument('id');
$schoolyear = $route->getArgument('sy');
    // $schoolyear = $request->getAttribute('sy');
    $db = new DbOperation();
    $payments = $db->getPayments($userid, $schoolyear);
    $response->getBody()->write(json_encode(array("payments" => $payments)));
});

^ This line of code will take the returned array result from getPayment() function then encode it to json.

The problem is, after testing my API in Postman, Postman only gives me this result

{"payments":[]} 

Please help me. Thank you. (Sorry for my bad english)

Find the answer. I misplace some variables.

The line $stmt->bind_param("ss", $userid, $schoolyear); should be written as $stmt->bind_param("ss", $schoolyear, $userid); .

Everything is working now. Thank you. :)

This thread is now CLOSED.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM