doutiaosu2310 2013-10-09 13:10
浏览 54
已采纳

从php中的数据库结果创建自定义json数组?

Hi I have some table say fro example table1 ,table2 in my mysql databse. I need to get following json result from php. Can anyone suggest how to achieve this? any good tutorial doing same is also helpful. I am able to convert database result in simple json response but custom response is something difficult for me.

{
response:ok
tables:[
{
    name:table name
        data:[
                {
                fieldname1:value1
                fieldname2:values2
                },
                {
                fieldname1:value1
                fieldname2:value2
                }
                .
                .
            ]               
},
{
    name:table name1
        data:[
                {
                fieldname1:value1
                fieldname2:values2
                },
                {
                fieldname1:value1
                fieldname2:value2
                }
                .
                .
            ]               
},

]
}
}
  • 写回答

1条回答 默认 最新

  • donglin5770 2013-10-09 13:18
    关注

    Quoting from How to convert mysql data base table data in json using php, once you have your table names you can do for each of them.

    $result = array();
    $result['response'] = 'ok'
    foreach ($tables as $tableName) {       
        $query = mysql_query("SELECT * FROM $tableName");
        $rows = array();
        while($row = mysql_fetch_assoc($query)) {
            $rows[] = $row;
        }
        $result['tables'][] = array(
             'name' = $tableName,
             'data' = $rows
        )
    }
    print json_encode($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大