2017-08-16 111 views
0

所以基本上,我有一個函數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。

的問題是,在郵差測試我的API後,郵差只給了我這樣的結果

{"payments":[]} 

請幫助我。謝謝。 (對不起,我的英語不好)

+0

你能確保你的MySQL查詢返回結果嗎? – Ice76

+0

@ Ice76是的,在phpmyadmin中試過這個查詢,它返回結果。 – WeShallCode

回答

-1

找到答案。 我錯位了一些變量。

行應寫爲$stmt->bind_param("ss", $schoolyear, $userid);

一切正在工作。謝謝。 :)

此線程現已關閉。