PHP while while循环,第二个循环只运行一次
我想借助while while循环打印信息,但它只运行一次。PHP while while循环,第二个循环只运行一次
while($nextDate<$currentDate)
{
$nextDate=date('Y-m-d',strtotime('+ 6 days',strtotime($weekDate)));
$qM="select count(*) as count ,Deposit.DepositNo, Deposit.DepositDate,
sum(DepositItem.Amount) as Amount,DAmount
from DepositItem
inner join Deposit on Deposit.DepositNo=DepositItem.DepositNo
where Deposit.DepositDate>='".$weekDate."' and Deposit.DepositDate<='".$nextDate."' group by Deposit.DepositNo
order by Deposit.DepositNo desc";
$connM=new dbconfig();
$connM->connectdb();
$connM->execute($qM);
$amt=0;
$damt=0;
while($rowsM =$connM->fetch_row())
{
$amt=$amt+$rowsM['Amount'];
$damt=$damt+$rowsM['DAmount'];
}
}
$ nextDate总是比$的currentdate更大,因为要添加6天
$nextDate=date('Y-m-d',strtotime('+ 6 days',strtotime($weekDate)));
不确定这是真的。 '$ weekDate'与'$ currentDate'相同吗? – 2013-02-22 07:44:58
不一定,我们不知道'$ weekDate'的价值。如果在我看来,第一次将运行一次,或无限循环,因为'$ weekDate'没有改变:) – 2013-02-22 07:45:07
@VladPreda嘿,这就是我所说 - 除了几乎一样好。 – 2013-02-22 07:46:20
您的代码来看,有太多的未知数找出问题。
但我写在代码中的一些评论指出潜在的问题/调试建议:
// what are the (expected) values of $nextDate and $currentDate ?
while ($nextDate < $currentDate) {
$nextDate = date('Y-m-d', strtotime('+ 6 days', strtotime($weekDate)));
// this makes the big loop either run once (if $weekDate is within the 6 days interval)
// or an infinite loop if it isn't
$qM = "select count(*) as count ,Deposit.DepositNo, Deposit.DepositDate,
sum(DepositItem.Amount) as Amount,DAmount
from DepositItem
inner join Deposit on Deposit.DepositNo=DepositItem.DepositNo
where Deposit.DepositDate>='" . $weekDate . "' and Deposit.DepositDate<='" . $nextDate . "' group by Deposit.DepositNo
order by Deposit.DepositNo desc";
// do an echo $qM and copy it in Phpmyadmin, see if it returns expected results
$connM = new dbconfig(); // not quite good to have these inside a loop
$connM->connectdb(); // you should put these 2 lines outside
$connM->execute($qM);
$amt = 0;
$damt = 0;
while ($rowsM = $connM->fetch_row()) { // do a var_dump($rowsM) inside the loop
$amt = $amt + $rowsM['Amount'];
$damt = $damt + $rowsM['DAmount'];
}
}
你没有第一while循环'}'缺失或关闭它只是拼写错误。 – 2013-02-22 07:42:03
我在第二个循环中看不到打印语句。 – 2013-02-22 07:42:27
你现在得到的是什么?还有你期望的o/p? – 2013-02-22 07:42:28