0

嗨所以我做了一个查询,当我尝试循环出结果时,它会一遍又一遍地循环出相同的变量。这不是一个无限循环,并且由于数据的原因,很难判断它是否循环了正确的次数。我正在尝试做的是:从客户表中选择所有不同的 customer_uid,其中 start_cycle_uid (来自 start_cycle 表)具有相同的 customer_home id 和相同的 start_date 传入。所以我会得到每个客户之一在该开始日期属于那个 customer_home。我得到的是相同 customer_uid 的列表。我的逻辑错了吗?

$homeQuery=$DB->prepare("select distinct customer_uid from customers
                            where start_cycle_uid =
                                (select uid from start_cycles
                                    where customer_home_uid=".$DB->quote_smart($_REQUEST['homeid'])."
                                    and start=".$DB->quote_smart($_REQUEST['start']).")");

$homeResult=$DB->query($homeQuery);
$homeRow=$DB->fetchArray($homeResult);  
if ($DB->numRows($homeResult) > 0) {
    for ($x=0; $x<=$DB->numRows($homeResult); $x++){
        echo $homeRow['customer_uid']."<br />";
    }
}

更新:当前代码如下所示:

$homeQuery=$DB->prepare("select distinct customer_uid from customers
                                where start_cycle_uid =
                                    (select uid from start_cycles
                                        where customer_home_uid=".$DB->quote_smart($_REQUEST['homeid'])."
                                        and start=".$DB->quote_smart($_REQUEST['start']).")");

    $homeResult=$DB->query($homeQuery);
    while($row = $DB->fetchArray($homeResult))
    {
       echo $row['customer_uid']."<br />";
    }

这现在陷入了一个无限循环。

4

2 回答 2

1

你的逻辑是多余的。常见的做法是在行存在时对其进行迭代 - 然后您不需要检索行数。它会像

while($row = $DB->fetchArray($homeResult))
{
   //now you have $row as actual fethed DB row
}

- 这将起作用,因为分配将返回与$DB->fetchArray($homeResult)- 即false当没有更多行时 - 这将导致循环结束。

于 2013-10-15T10:12:53.333 回答
0

要获得下一行,您必须$homeRow = $DB->fetchArray($homeResult);在循环内移动。现在它只得到第一行,因为你调用它一次。

$DB->numRows($homeResult)将变量放入变量以避免每次循环运行都是个好主意。

于 2013-10-15T10:10:30.823 回答