Paulqvz Posted February 12, 2020 Share Posted February 12, 2020 I have below script. it does the while loop 100%. it updates the mysql database one at a time as it should. the problem I have now is that the while loop does not end and go to the next statement as it should. it keeps pollong the database. so when in back end you change to 0 it automatically updates again. Please see if you can help me to see where I can stop this while loop when there are no more loops // Here I select the amount of rows $sql_query = "SELECT ae FROM `debitorderrejectionimport` WHERE ae = '0'"; $rowCount = mysqli_query($conn,$sql_query); $rowCountUpdate = mysqli_num_rows($rowCount); echo $rowCountUpdate; while($rowCountUpdate > 0) { $sql = "UPDATE `ttee`.`au1` INNER JOIN `ttee`.`au` ON (`au1`.`id` = `au`.`id`) INNER JOIN `ttee`.`ae1` ON (`ae1`.`idd` = `au1`.`idd`) INNER JOIN `ttee`.`debitorderrejectionimport` ON (`debitorderrejectionimport`.`nr` = `ae1`.`id`) SET `au`.`amount` = `ae1`.`amount` + `au1`.`amount`, `debitorderrejectionimport`.`ae` = au.id ;"; $result = mysqli_query($conn, $sql); $updated = mysqli_affected_rows($conn); $rowCountUpdate - ($updated);} // if it finished updating and there is no more rows it must continue with below query mysqli_query($conn, " INSERT INTO `sataxicrm754`.`debitorderrejectionimport_back` ( `Outbound`, `Allocation`, `AccountName`, `QueryComplaintType`, `QueryStatus`, `Querytypeoption`, `Description`, `DealID`, `Deals`, `Assignedusername`, `Teams`, `CampaignName`, `CampaignID`, `inserted`, `idnumber`, `nr`, `datew`, `premium`, `policynumber`, `ContactNumber`, `CollectionType`, `OpportunityAmount`, `Broker`, `impref`, `id` ) SELECT `Outbound`, `Allocation`, `AccountName`, `QueryComplaintType`, `QueryStatus`, `Querytypeoption`, `Description`, `DealID`, `Deals`, `Assignedusername`, `Teams`, `CampaignName`, `CampaignID`, `inserted`, `idnumber`, `nr`, `datew`, `premium`, `policynumber`, `ContactNumber`, `CollectionType`, `OpportunityAmount`, `Broker`, `impref`, `id` FROM `sataxicrm754`.`debitorderrejectionimport` WHERE QueryComplaintType <> 'QueryComplaintType' "); Quote Link to comment https://forums.phpfreaks.com/topic/310019-while-loop-not-stopping/ Share on other sites More sharing options...
gw1500se Posted February 12, 2020 Share Posted February 12, 2020 (edited) You need to change your loop from a while to a for. for(i=0; i<$rowCountUpdate; i++) What seems strange to me is that you are not changing your query parameters on each pass. It looks like you are always updating the same row thus the infinite loop. Edited February 12, 2020 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/310019-while-loop-not-stopping/#findComment-1574229 Share on other sites More sharing options...
mconte Posted February 12, 2020 Share Posted February 12, 2020 (edited) 2 hours ago, Paulqvz said: $rowCountUpdate - ($updated);} you'd want to use: $rowCountUpdate -= ($updated);} Edited February 12, 2020 by mconte Quote Link to comment https://forums.phpfreaks.com/topic/310019-while-loop-not-stopping/#findComment-1574230 Share on other sites More sharing options...
gw1500se Posted February 12, 2020 Share Posted February 12, 2020 Good catch. I missed that subtlety. Quote Link to comment https://forums.phpfreaks.com/topic/310019-while-loop-not-stopping/#findComment-1574233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.