基於以下答案更新:有道用PHP創建JSON數據/ MySQL的
基於下面的答案,我現在有以下PHP腳本:
header('Content-type:application/json');
function getdata($the_query)
{
$connection = mysql_connect('server', 'user', 'pass') or die (mysql_error());
$db = mysql_select_db('db_name', $connection) or die (mysql_error());
$results = mysql_query($the_query) or die(mysql_error());
header('Content-type:application/json');
$the_data['rss']['channels']['title'] = $title;
$the_data['rss']['channels']['link'] = $link;
$the_data['rss']['channels']['description'] = $description;
while($row = mysql_fetch_array($result))
{
extract($row);
$the_data['rss']['channels']['items']['title'] = $item_title;
$the_data['rss']['channels']['items']['link'] = "$item_link;
$the_data['rss']['channels']['items']['date'] = $item_date;
$the_data['rss']['channels']['items']['description'] = $item_description;
}
mysql_close($connection);
return json_encode($the_data);
}
它返回以下:
{
"rss":
{
"channels":
{
"title":"title goes here",
"link":"link goes here",
"description":"description goes here",
"items":
{
"title":"'title goes here",
"link":"link goes here",
"date":"date goes here",
"description":"description goes here"
}
}
}
}
它應該基於從數據庫返回的行數返回很多項目,爲什麼我只獲得1個項目?
您從MySQL獲取數據到一個數組中,然後運行'json_encode($ your_array)'就完成了。 – 2011-06-02 10:03:10
你的輸出將不會工作順便說一句。你有幾個'item'元素。我假定這個JSON被解析時,只有最後一個會「生存」。您不能使用同一個鍵擁有多個條目。看起來你想創建一個'item'條目,每個條目都是數組的一個元素... – 2011-06-02 10:07:25
請根據答案查看更新後的問題。 – oshirowanen 2011-06-02 10:33:11