将oci_fetch_assoc数据转换为json时出现问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将oci_fetch_assoc数据转换为json时出现问题相关的知识,希望对你有一定的参考价值。
我试图从Oracle表中获取数据并转换为JSON对象,以便可以在Jquery Datatable上读取它。这是我的php代码 -
<?php
$db = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1521)(SEND_BUF_SIZE=)(RECV_BUF_SIZE=))(LOAD_BALANCE=yes))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=servicename)))" ;
if($c = OCILogon("test", "test", $db))
{
$rowfetch = oci_parse($c, "select * from table" );
oci_execute($rowfetch);
while($row=oci_fetch_assoc($rowfetch))
{
$arr['data'][]= $row;
$senddata= json_encode($arr, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
echo $senddata;
}
}
else
{
$err = OCIError();
echo "Connection failed." . $err[text];
}
OCILogoff($c);
?>
我得到像这样的JSON响应 -
{ “数据”:[{ “SCHEMA”: “TEST”, “TABLE_NAME”: “TEST_T”, “CUSTOMFIELD10”: “创建”, “CUSTOMFIELD5”: “NIK”, “CUSTOMFIELD9”: “NIK”, “LAST_UPD_TIME” : “27-JUN-18”}]} { “数据”:[{ “SCHEMA”: “TEST”, “TABLE_NAME”: “TEST_T”, “CUSTOMFIELD10”: “创建”, “CUSTOMFIELD5”: “NIK”, “CUSTOMFIELD9”:“NIK”,“LAST_UPD_TIME”:“27-JUN-18”},{“SCHEMA”:“TEST”,“TABLE_NAME”:“ART_CM3_T”,“CUSTOMFIELD10”:“平均DWP”,“CUSTOMFIELD5” :NULL, “CUSTOMFIELD9”: “结算”, “LAST_UPD_TIME”: “05-FEB-19”}]} { “数据”:[{ “SCHEMA”: “TEST”, “TABLE_NAME”: “TEST_T”,“CUSTOMFIELD10 “:” 创建 “ ”CUSTOMFIELD5“: ”NIK“, ”CUSTOMFIELD9“: ”NIK“, ”LAST_UPD_TIME“: ”27-JUN-18“},{ ”模式“: ”TEST“, ”TABLE_NAME“:” ART_CM3_T “,”CUSTOMFIELD10“:”平均DWP“,”CUSTOMFIELD5“:null,”CUSTOMFIELD9“:”BILLING“,”LAST_UPD_TIME“:”05-FEB-19“},{”SCHEMA“:”TEST“,”TABLE_NAME“ : “ART_T”, “CUSTOMFIELD10”: “创建”, “CUSTOMFIELD5”: “NIK”, “CUSTOMFIELD9”: “NIK”, “LAST_UPD_TIME”: “09-FEB-19”}]} { “数据”:[{ “纲目”: “TEST”, “表格名”: “TEST_T”, “CUSTOMFIELD10”: “创建”, “CUSTOMFIELD5”: “NIK”, “CUSTOMFIELD9”: “NIK”,“LAST_UPD_TIME “:”27-JUN-18“},{”SCHEMA“:”TEST“,”TABLE_NAME“:”ART_CM3_T“,”CUSTOMFIELD10“:”平均DWP“,”CUSTOMFIELD5“:null,”CUSTOMFIELD9“:”BILLING“ “LAST_UPD_TIME”: “05-FEB-19”},{ “模式”: “TEST”, “TABLE_NAME”: “ART_T”, “CUSTOMFIELD10”: “创建”, “CUSTOMFIELD5”: “NIK”, “CUSTOMFIELD9” : “NIK”, “LAST_UPD_TIME”: “09-FEB-19”},{ “模式”: “TEST”, “TABLE_NAME”: “ART_CUST”, “CUSTOMFIELD10”: “创建”, “CUSTOMFIELD5”: “NIK” “CUSTOMFIELD9”: “NIK”, “LAST_UPD_TIME”: “09-FEB-19”}]}
但这不是Datatable可以读取和接受的正确格式。对于Jquery Datatable,JSON Response应该是这样的 -
{“data”:[[“Tiger Nixon”,“System Architect”,“Edinburgh”,“5421”,“2011/04/25”,“$ 320,800”],[“Garrett Winters”,“Accountant”,“Tokyo” “,”8422“,”2011/07/25“,”170,750美元“,[”Herrod Chandler“,”销售助理“,”旧金山“,”9608“,”2012/08/06“,”137,500美元“ ],[“Rhona Davidson”,“整合专家”,“东京”,“6200”,“2010/10/14”,“$ 327,900”],[“Colleen Hurst”,“javascript开发者”,“旧金山”, “2360”,“2009/09/15”,“$ 205,500”,[“Haley Kennedy”,“高级营销设计师”,“伦敦”,“3597”,“2012/12/18”,“313,500美元”], [“Tatyana Fitzpatrick”,“区域主任”,“伦敦”,“1965”,“2010/03/17”,“$ 385,750”]]}
我在这做错了什么?如何将此JSON格式更改为所需的Datatable格式?
在追加返回记录后的while循环中,它是回显它的。
除了返回错误的格式,即返回重复记录。
您可以在编码后输出单个行,并输出包含它们的编码JSON数组。
echo '{"data": [';
$row = oci_fetch_row($rowfetch);
while ($row) {
$senddata = json_encode($row, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
echo $senddata;
$row = oci_fetch_row($rowfetch);
if ($row === FALSE) break;
echo ',';
}
echo ']}';
// Remember to free the statement and close the connection.
您还可以构建编码记录数组并完全输出。
$records = [];
oci_fetch_all($rowfetch, &$records, null, null, OCI_FETCHSTATEMENT_BY_ROW + OCI_NUM);
$resp = ["data" => $records];
echo json_encode($resp, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
// Remember to free the statement and close the connection.
以上是关于将oci_fetch_assoc数据转换为json时出现问题的主要内容,如果未能解决你的问题,请参考以下文章