【问题标题】:Create JSON output from SQL Server Joined Resultset从 SQL Server Joined Resultsset 创建 JSON 输出
【发布时间】:2018-12-04 02:52:50
【问题描述】:

我正在尝试使用 PHP 创建 Web 服务。我的数据来自 SQL Server 数据库。我要使用的查询输出来自内部联接。同一天有多行(日期列)。这些稍后会根据 json 输出中的日期进行分组。

我对使用 PHP 的了解不多,我喜欢学习任何可以从 SQL 数据创建嵌套 JSON 输出的方法,如下所示。

[
{
    "date": "2018-11-09 18:30:00",
    "details": [{
        "ServerName":"Server1",
        "ScheduleStart":"2018-11-09 08:00:00",
        "ScheduleEnd": "2018-11-09 11:00:00"
    },
    {
        "ServerName":"Server2",
        "ScheduleStart":"2018-11-09 18:00:00",
        "ScheduleEnd": "2018-11-09 21:00:00"
    },
    {
        "ServerName":"Server3",
        "ScheduleStart":"2018-11-09 21:00:00",
        "ScheduleEnd": "2018-11-10 00:00:00"
    }
    ],
    "total":3,
    "summary":[{
        "ServerName": "Server1",
        "Status": "Success"
    },
    {
        "ServerName": "Server2",
        "Status": "Failed"
    },
    {
        "ServerName": "Server3",
        "Status": "Scheduled"
    }
    ]
},
{
    "date": "2018-11-10 18:30:00",
    "details": [{
        "ServerName":"Server3",
        "ScheduleStart":"2018-11-09 21:00:00",
        "ScheduleEnd": "2018-11-10 00:00:00"
    },
    {
        "ServerName":"Server4",
        "ScheduleStart":"2018-11-10 02:00:00",
        "ScheduleEnd": "2018-11-10 05:00:00"
    }
    ],
    "total":2,
    "summary":[{
        "ServerName": "Server3",
        "Status": "Success"
    },
    {
        "ServerName": "Server4",
        "Status": "Scheduled"
    }
    ]
}
]

请指导我如何开始。我已经探索了几种方法,但没有什么能让我有足够的信心来创建类似的输出。

json_encode 给了我如下的普通 json。但是,我实际上想要上面已经显示的嵌套输出。

[
    {
        "date": "2018-05-27 00:00:00.000",
        "ServerName": "Server1",
        "ScheduleStart": "2018-05-27 03:00:00.000",
        "ScheduleEnd": "2018-05-27 06:00:00.000",
        "Status": "Scheduled"
    },
    {
        "date": "2018-05-27 00:00:00.000",
        "ServerName": "Server2",
        "ScheduleStart": "2018-05-27 21:30:00.000",
        "ScheduleEnd": "2018-05-28 00:30:00.000",
        "Status": "Scheduled"
    }
]

我使用 SQL 2014,它不允许来自 SQL 的嵌套 JSON 输出,在这种情况下会容易得多。

或者,我相信,可以多次调用数据库来收集和创建数组,这看起来不是一个好习惯。

【问题讨论】:

标签: php json sql-server


【解决方案1】:

好的,经过一些研究和试验,我得到了我想要的 JSON 格式。以下是相同的示例。

$items= array();
    //Sample SQL statement, its syntactically wrong. Just to omit references. 
    $sql = "select [date], [ServerName], [ScheduleStart], [ScheduleEnd], [Status] from table1 tb1 inner join table2 tb2 on tb1.Subscribe_ID = tb2.Subscribe_ID order by date";

    if($stmt = sqlsrv_query($connect, $sql))
    {
        $Checks = sqlsrv_has_rows( $stmt );
        if ($Checks === true)
        {
            while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
            {
                $date = $row['date'];
                if (isset($items[$date])){
                    $item = $items[$date];
                }
                else
                {
                    $item = array
                    (
                    'date' => $date,
                    'details' => array(),
                    'total'   => 1,
                    'summary' => array()
                    );
                }
                $item['details'][] = array(
                    'ServerName'=>$row['ServerName'],
                    'ScheduleStart'=>$row['ScheduleStart'],
                    'ScheduleEnd'=>$row['ScheduleEnd']
                );
                $item['total'] = count($item['details']);
                $item['summary'][] = array(
                                            'ServerName'=>$row['ServerName'],
                                            'Status'=> $row['Status']
                                        );
                $items[] = $item;
            }

            return success($items);
        }
        else
        { 
                return error('Data not found!','404');
        }       
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    相关资源
    最近更新 更多