PHP无限循环

时间:2012-07-29 21:00:13

标签: php mysqli while-loop

我一直试图弄清楚我的PHP脚本中是如何获得无限循环的。我知道的自定义函数没有任何while循环,这些只有两个有0个或更多循环。 (我考虑过将它们结合起来,但我会等到我解决这个问题)

传递$ query和$ result的原因是我可以获取对象,实际的查询是

$ query = $ db-> query(“SELECT UNIX_TIMESTAMP(Fitting Date),Retail PricePatient IDMarketingMono or Bi FROM拟合WHERE YEAR(Fitting Date)='“。$ _REQUEST ['year']。”'ORDER BY Fitting Date“);

function printcontent ($result,$query,$month) {
    global $totalhearingaids, $totalincomeoverall;
    $hearingaids = 0;
    $totalincome = 0.00;
    $monthdate = date(n,$result->{"Fitting Date"});
    echo '<div id="innercontent"><table>'. "\n";
    while ($monthdate = $month)
    {
    echo '<tr>' . "\n";
    echo '<td>' . unixtodate($result->{"Fitting Date"}). '</td><td>' . printname($result->{"Patient Id"}) . "</td><td>" . $result->{"Mono or Bi"} . "</td><td>" . $result->{"Retail Price"} . "</td><td>" . printmarketing($result->{"Marketing"}) . "</td><br />" . "\n";
    echo '</tr>' . "\n";
    $hearingaids += $result->{"Mono or Bi"};
    $totalincome += $result->{"Retail Price"};
    $result = $query->fetch_object();
    $monthdate = date(n,$result->{"Fitting Date"});
}
$totalhearingaids += $hearingaids;
$totalincomeoverall += $totalincome;
echo '<br />' . "\n" . '<b id="yeartitle">Sum</b>' . '<tr>' . "\n" . '<td></td><td></td><td>' . $hearingaids . '</td><td>$' . $totalincome . '</td><br />' . "\n" . '</table></div><br />' . "\n";

}

这个发生在代码的主要“功能”中。

$month = 1;
while ($month <= 12){
                echo '<i>' . date(M,$month) . '</i><br />' . "\n";
                printcontent($object,$query,$month);
                $month += 1;
            }

1 个答案:

答案 0 :(得分:3)

while ($monthdate = $month)

$monthdate设置为$month的值,该值始终为大于1的值,即true

function printcontent ($result,$query,$month) {
    $monthdate = date(n,$result->{"Fitting Date"});
    // $monthdate is set to the value of $month, which results in a > 0 value (evaluates to true)
    while ($monthdate = $month) { }
}

$month = 1;
while ($month <= 12){
    printcontent($object,$query,$month);
    $month += 1;
}

您的代码与以下内容具有相同的效果:

function printcontent ($result,$query,$month) {
    while ($month) { }
}

printcontent(null, null, true);

为防止您的printcontent功能无限循环,请检查以确保$ month&lt; = 12。