PHP& sqlsrv - 从结果创建数组,而不知道列名?

时间:2016-10-05 04:12:37

标签: php arrays sqlsrv

使用下面的内容,我回显了结果的JSON数组。但是这需要我确定我想从SQL查询返回的列名:

$new_sql = "SELECT TOP 200 * FROM STracker ORDER BY [ID] DESC";

$check_statement = sqlsrv_query($conn, $new_sql);
$data = array();

while($row = sqlsrv_fetch_array($check_statement, SQLSRV_FETCH_ASSOC)) {             

$data['data'][] = array(

    'id'                        => $row['ID'],
    's_reference'               => $row['s_reference'],
    'reference'                 => $row['reference'],                                   
    'customer_name'             => $row['customer_name']

);                            

}

有没有办法创建该数组信息,但是动态返回查询返回的所有列?因此,通过使用 SELECT * FROM ,所有列数据都在数组中返回,但是我不需要单独写出所有这些数据? (下面)

    'id'                        => $row['ID'],
    's_reference'               => $row['s_reference'],
    'reference'                 => $row['reference'],                                   
    'customer_name'             => $row['customer_name']

好的,我忘了补充说我试过这个:

$ data [' data'] [] = array($ row);

这显然是错误的,使用以下内容后,效果非常好!

$ data [' data'] [] = $ row;

0 个答案:

没有答案