如何打印JSON数据mysql?

时间:2018-04-22 17:06:21

标签: mysql json

我只是学习MySQL和PHP。我有来自MySQL的echo JSON的问题。 任何人,请帮我解决。非常感谢你

我有2张桌子。表格频道和频道详情

Table 1: channel
id|name
1 |channel 1
2 | channel 2

Table 2: detail
id| detail | cid (get from id table 1)
1 | detail 1
2 | detail 2

我尝试回复JSON,但输出错误我想要 这是我的代码PHP并选择MySQL

 $query="SELECT * FROM channel, detail WHERE channel.id=detail.cid";
$resouter = mysql_query($query);
$set = array();
$total_records = mysql_num_rows($resouter);
if($total_records >= 1){ 
  while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)){
    $set['channels'][] = $link;
  }
}

 echo $val= str_replace('\\/', '/', json_encode($set));

我想要那样的输出

{

"data": [
    {
        "id": 1,
        "title": "channel 1",
        "chanels": [
            {
                "id": 1,
                "channel.cid": 1,
                "title": "detail 1"
            },
            {
                "id": 2,
                "channel.cid": 1,
                "title": "detail 2"
            }
        ]
    },
    {
        "id": 2,
        "title": "channel 2",
        "chanels": [
            {
                "id": 1,
                "channel.cid": 1,
                "title": "detail 1"
            },
            {
                "id": 2,
                "channel.cid": 1,
                "title": "detail 2"
            }
        ]
    }
]
}

0 个答案:

没有答案