我要輸出的MySQL表通過以下方式的一些記錄與resCode
和resText
在JSON
:如何在PHP中以正確的JSON格式輸出?
{
"data":[
{
"id":"44",
"month":"January",
"income":"2500",
"expanse":"0"
},
{
"id":"45",
"month":"February",
"income":"5500",
"expanse":"400"
},
{
"id":"47",
"month":"March",
"income":"25000",
"expanse":"11000"
}
],
"resCode":"200",
"resText":"SUCCESS"
}
因爲我很新的PHP數組,我無法在服務器側部格式化爲輸出如上。另外我不知道如何輸出resCode和resText。
同樣的任何幫助將不勝感激。
PHP部分:
<?php
include_once 'includes/db_connect.php';
?>
<?php
$stmt = $mysqli->prepare("SELECT * FROM records");
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
// get variables from result.
$stmt->bind_result($id, $month, $income, $expanse);
while ($stmt->fetch()) {
$data[]=array(id=>$id, month=>$month, income=>$income, expanse=>$expanse);
}
$response["data"] = $data;
$response["resCode"] = "200";
echo json_encode($response);
?>
也請包括你的代碼目前輸出 –
@SamiKuhmonen提示錯誤。至於我告訴你,我不不知道輸出的正確方法。 –