(PHP)如何防止丢弃ODBC结果集? [英] (PHP) How to Prevent Discarding of an ODBC Result Set?

查看:75
本文介绍了(PHP)如何防止丢弃ODBC结果集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个Web应用程序,我试图在其中创建我必须使用odbc_exec来收集两个不同查询的结果,然后创建一个包含两个查询的组合信息的JSON文件的地方.

So I have a web app that I'm trying to create where I have to use odbc_exec to gather the results of two different queries and then create a JSON file with combined info from the two queries.

下面的示例(省略了连接和查询) ...

$result = odbc_exec($c, $q);
$result1 = odbc_exec($c, $q1);
$resultRows = array();
$response = array();

while($row = odbc_fetch_array($result)) {
    $tempResult = $result1;
    $value = "0";
    $other = $row['FIELD'];
    while($row1 = odbc_fetch_array($tempResult)) {
        if($row['FIELD'] == $row1 ['FIELD']) {
            $value = $row1['FIELD'];
        }
    }
    if($value != "0") {
        $resultRows[] = array('FIELD'=>$value, 'OTHER'=>$other);
    }
}

$response['data'] = $resultRows;

$fp = fopen('somefile.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);

此问题是,在第一个循环通过后,它将停止进入嵌套的while循环.我知道odbc_fetch_array会从结果集中删除数据,这就是为什么我试图创建对结果集的引用,该引用在每次大循环后都会重置,但是仍然不能解决我的问题.

The problem with this is that it stops going into the nested while loop after the first loop through. I know that odbc_fetch_array removes the data from the results set which is why I attempted to create a reference to the result set that resets after each big loop, but that still doesn't resolve my problem.

任何信息都将非常有帮助!预先感谢!

Any info would be extremely helpful! Thanks in advance!

推荐答案

$tempResult = $result1;不会对对象进行深层复制,只是通过引用原始对象进行复制,因此当您以后调用odbc_fetch_array($tempResult)时,它确实是与odbc_fetch_array($result1)相同,这意味着您只能拥有一个对象.因此,对odbc_fetch_array的所有后续调用都将在这两个变量上耗尽.您可以每次clone该对象,但我认为一种更有效的方法是对其进行一次迭代并将值保存到数组中.然后,您可以在嵌套循环中重新遍历数组.

$tempResult = $result1; does not make a deep copy of the object, just a copy by reference to the original object, so when you later call odbc_fetch_array($tempResult) it's really the same thing as odbc_fetch_array($result1) which means you only ever have one object. So any subsequent calls to odbc_fetch_array will be exhausted on either variable. You could clone the object each time, but I think a much more efficient approach would be to iterate through it once and save the values to an array. Then you could re-iterate over the array in your nested loop.

$result = odbc_exec($c, $q);
$result1 = odbc_exec($c, $q1);
$resultRows = array();
$response = array();

// save this to a regular array for re-use later
$innerQueryResult = array();
while($rowTemp = odbc_fetch_array($result1)) {
    $innerQueryResult []= $rowTemp;
}

while($row = odbc_fetch_array($result)) {
    $value = "0";
    $other = $row['FIELD'];

    // iterate through the inner query result set
    foreach ($innerQueryResult as $row1) {
        if($row['FIELD'] == $row1 ['FIELD']) {
            $value = $row1['FIELD'];
        }
    }
    if($value != "0") {
        $resultRows[] = array('FIELD'=>$value, 'OTHER'=>$other);
    }
}

$response['data'] = $resultRows;

$fp = fopen('somefile.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);

这篇关于(PHP)如何防止丢弃ODBC结果集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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