我有一個我寫的腳本來返回數據庫外的記錄。實際上有兩條記錄時,我爲mysql查詢返回一條記錄。這就是我回到:在PHP數組中顯示多行
{ "cases": [ {"name":"Test Case for App","number":"3846"}] }
我應該看到:
{ "cases": [ {"name":"Test Case for App","number": "2903"}, {"name":"Test Case 2","number": "2856"} ] }
這裏是我的源:
$sql = "select * from cases as c join contacts_cases as conc on c.id = conc.case_id where conc.contact_id = '1b360507'";
$query = mysql_query($sql);
// If we find a match, create an array of data, json_encode it and echo it out
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_assoc($query);
$response = array(
'name' => $row['name'],
'number' => $row['case_number']
);
echo '{ "cases": [ ', json_encode($response), "] }";
您可能想要在那裏使用循環來獲取所有值。 – inhan 2012-07-12 17:34:51