基本上,我有一个函数getPayments()。此函数应执行查询,从多个表中选择(已连接)。这是我的代码
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;
}
在我的index.php文件中:
//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)));
});
^这行代码将从getPayment()函数获取返回的数组结果,然后将其编码为json。
问题是,在Postman中测试我的API之后,Postman只给了我这个结果
{"payments":[]}
请帮帮我。谢谢。 (抱歉我的英语不好)
答案 0 :(得分:-1)
找到答案。 我错放了一些变量。
行$stmt->bind_param("ss", $userid, $schoolyear);
应写为$stmt->bind_param("ss", $schoolyear, $userid);
。
现在一切正常。谢谢。 :)
此主题现已关闭。