简体   繁体   中英

PHP double while loop, second loop is running only once

I want to print information with the help of second while loop, but it is running only once.

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))); 

Judging by your code, there are too many unknowns to figure out the issue.

But I wrote some comments in the code to point out potential problems / debugging suggestions:

// 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'];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM