2017-06-15 126 views
-1

我的問題是從SQL獲取數據,動態地更新圖表 MySQL數據就像從PHP獲取MySQL數據到JSON

id | age 
1 | 12 
2 | 21 
3 | 31 
4 | 11 
5 | 31 

,我想呼應這樣

[1,12] , [2,21] , [3,31] , [4,11] , [5,31] 

我有嘗試這樣

$sql = "SELECT id, age FROM tes"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    while($row = mysqli_fetch_array($result)) 
    { 
     $id = $row['id']; 
     $tes = $row['tes']; 
     $a = array($id,$tes); 
     echo json_encode($a, JSON_PRETTY_PRINT); 
    } 

,但最終就像

[1,12] [2,21] [3,31] [4,11] [5,31] 

請幫我...

+0

您所需的輸出無效JSON。這正是你想要的嗎? – CollinD

+0

如果連接JSON字符串,則不會獲得JSON,就像拼接JPEG圖片不會產生更大的圖片一樣。 –

回答

0

收集所有的行到一個2-d數組,然後返回,作爲JSON。

$a = array(); 
while($row = mysqli_fetch_array($result)) 
{ 
    $id = $row['id']; 
    $tes = $row['tes']; 
    $a[] = array($id,$tes); 
} 
echo json_encode($a, JSON_PRETTY_PRINT); 
+0

非常感謝你的兄弟..它適合我,你讓我的一天... –