获取从PHP ADO连接返回到MS-Access DB的JSON [英] Get JSON returned from PHP ADO connection to MS-Access DB

查看:53
本文介绍了获取从PHP ADO连接返回到MS-Access DB的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从MS-Access DB查询返回JSON字符串.我得到了记录集($ rs)很好,但是试图遍历以返回JSON,却不知道记录集中的所有字段.以下返回我想要的内容,但在[["aaa":"bbb","ccc":"ddd"]]中而不是我要查找的[{ ... }]中.

I'm trying to get JSON string returned from MS-Access DB Query. I get the recordset ($rs) fine, but trying to loop through to return JSON, without knowing all the fields in recordset. The following returns what I want but in [["aaa":"bbb","ccc":"ddd"]] not the [{ ... }] i'm looking for.

    $num_columns = $rs->Fields->Count ();
//echo $num_columns . "<br>";

$arrColumns = array();
for ($i=0; $i < $num_columns; $i++) {
    $arrColumns[] = $rs->Fields($i);
}

$arrResult = array();

while (!$rs->EOF) {
    $arrRow = array();
    for ($i=0; $i < $num_columns; $i++) {
        $arrRow[] = $arrColumns[$i]->value;
    }
    $arrResult[] = $arrRow;
    $rs->MoveNext();
}
//var_dump($arrResult);
echo json_encode($arrResult);
//print_r($arrResult);

[更新]:

完成了动态部分,但结果仍然存在问题.因为我是json_encode($Array),所以我无法按照自己的意愿使用JQuery获得结果.

accomplished the dynamic part but still have issue with results. Since i'm json_encode($Array) i'm not able to get results with my JQuery as I would like.

$num_columns = $rs->Fields->Count ();
$arrColumns = array();

for ($i=0; $i < $num_columns; $i++) {
    $arrColumns[] = $rs->Fields($i);
    $newArr[] = $rs->Fields($i)->name; 
}

$arrResult = array();

while (!$rs->EOF) {
    $arrRow = array();
    for ($i=0; $i < $num_columns; $i++) {
       $arrRow[$newArr[$i]] = $arrColumns[$i]->value;
    }
    $arrResult[] = $arrRow;

    $rs->MoveNext();
}

echo $_GET['callback'] . '(' . json_encode($arrResult) . ')';

我的JSON返回如下:[{"First":"John"},{"Last":"Doe"}] 包装在对象中的数组.我需要返回一个对象或数组,以便可以在客户端进行处理,例如:{"First":"John"},{"Last":"Doe"}

My JSON gets returned like: [{"First":"John"},{"Last":"Doe"}] An array wrapped in an object. I need to return either an object or an array so I can handle in my client side like: {"First":"John"},{"Last":"Doe"}

    <script>
        $.getJSON('http://remote.domain.com/json.php?callback=?',function(res){
            alert('Results: '+res.Last);
        });
    </script>

我认为这可能是我json_encode($Array);而不使用课程的方式吗?

I think it might be the way I'm json_encode($Array); and not using a class?

推荐答案

如果要以[{...}]格式返回,则需要将$arrRow值从数组更改为类.为简单起见,您可以利用 stdClass()

If you want to return in the [{...}] format, you will need to change the $arrRow value from array into class. To make it simple, you can leverage the use of stdClass()

尝试这样的事情:

while (!$rs->EOF) {
    $arrRow = array();

    $class = new stdClass();
    $class->field1 = $arrColumns[1]->value;
    $class->field2 = $arrColumns[2]->value;

    $arrRow[] = $arrColumns[$i]->value;

    $arrResult[] = $arrRow;
    $rs->MoveNext();
}

echo json_encode($arrResult);

这篇关于获取从PHP ADO连接返回到MS-Access DB的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆