ChrisMartino Posted August 7, 2010 Share Posted August 7, 2010 So I've coded this to run the while loop once for every server within the the table, there is currently two rows within the table and the while loop wont stop running it just keeps on running until I restart Apache, its producing the same effect as what "while(1)" would do, could anybody tell me what the problem could be?, thanks for your time in advance. class bot_restart { function __construct( ) { while($CurrentServers = mysql_fetch_array(mysql_query("SELECT * FROM xhost_boxs"))) { echo $CurrentServers['box_id']; } } } Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/ Share on other sites More sharing options...
Ruzzas Posted August 7, 2010 Share Posted August 7, 2010 Use print_r(mysql_fetch_array(mysql_query("SELECT * FROM xhost_boxs")); to show your full array and make sure that doesn't loop either, if it does then it may be the class. I'm not that good with classes, just custom functions Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096315 Share on other sites More sharing options...
wildteen88 Posted August 7, 2010 Share Posted August 7, 2010 Move mysql_query("SELECT * FROM xhost_boxs") outside of the loop. So its $result = (mysql_query("SELECT * FROM xhost_boxs"); while($CurrentServers = mysql_fetch_array($result)) { echo $CurrentServers['box_id']; } Otherwise you've got yourself an infinity loop, as PHP will keep on executing the query on every loop. Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096316 Share on other sites More sharing options...
ChrisMartino Posted August 7, 2010 Author Share Posted August 7, 2010 Move mysql_query("SELECT * FROM xhost_boxs") outside of the loop. So its $result = (mysql_query("SELECT * FROM xhost_boxs"); while($CurrentServers = mysql_fetch_array($result)) { echo $CurrentServers['box_id']; } Otherwise you've got yourself an infinity loop, as PHP will keep on executing the query on every loop. Hmm, I've just tried your method and it appears its still doing it? (I'll just paste the full file in-case you see a error elsewhere. <?php /* =================================== X-Host Restart bot This handles Auto-Restarts =================================== Bot Information ---------------------- This bot will run every minute to check server's are running. =================================== */ // Set the time limit so it doesn't max out. set_time_limit(0); // We include all the core class functionality. include('../PanelCore/panel_core.php'); // Start a new instance of the core. $Module['Core'] = new panel_core(); // We globalize $Module. global $Module; $RestartBot = new bot_restart(); class bot_restart { function __construct( ) { $ServersReturned = mysql_query("SELECT * FROM xhost_boxs"); while($CurrentServers = mysql_fetch_array($ServersReturned)) { echo $CurrentServers['box_id']; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096328 Share on other sites More sharing options...
Ruzzas Posted August 7, 2010 Share Posted August 7, 2010 Try class bot_restart { function __construct( ) { $ServersReturned = mysql_query("SELECT * FROM xhost_boxs"); $CurrentServers = mysql_fetch_array($ServersReturned); while($CurrentServers !== false) { echo $CurrentServers['box_id']; } } } Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096335 Share on other sites More sharing options...
ChrisMartino Posted August 7, 2010 Author Share Posted August 7, 2010 Try class bot_restart { function __construct( ) { $ServersReturned = mysql_query("SELECT * FROM xhost_boxs"); $CurrentServers = mysql_fetch_array($ServersReturned); while($CurrentServers !== false) { echo $CurrentServers['box_id']; } } } Seems to still run continuously with the code you provided, thanks for your input though its appreciated we all learn at some stage! Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096338 Share on other sites More sharing options...
wildteen88 Posted August 7, 2010 Share Posted August 7, 2010 The code posted by Ruzzas is not the correct wayto loop through the results returned from a query. My suggestion is the correct way. If that is causing an infinity loop then I dough it is the problem code, how do you know that is causing the problem? Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096344 Share on other sites More sharing options...
Ruzzas Posted August 7, 2010 Share Posted August 7, 2010 The code posted by Ruzzas is not the correct wayto loop through the results returned from a query. My suggestion is the correct way. If that is causing an infinity loop then I dough it is the problem code, how do you know that is causing the problem? Yes indeedy, What is this? $Module['Core'] = new panel_core(); Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096346 Share on other sites More sharing options...
ChrisMartino Posted August 7, 2010 Author Share Posted August 7, 2010 The code posted by Ruzzas is not the correct wayto loop through the results returned from a query. My suggestion is the correct way. If that is causing an infinity loop then I dough it is the problem code, how do you know that is causing the problem? Yes indeedy, What is this? $Module['Core'] = new panel_core(); That's starting a new instance of the class that controls other parts of the system Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096347 Share on other sites More sharing options...
wildteen88 Posted August 7, 2010 Share Posted August 7, 2010 I dont think the code we are seeing is the problem. As my code suggestion should not be causing an infinity loop no matter may many rows you have in your table. I think the issue lies elsewhere in your code. Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096350 Share on other sites More sharing options...
ChrisMartino Posted August 7, 2010 Author Share Posted August 7, 2010 Very strange, I was testing these scripts locally (WAMP Testing Server) and it was that bugging up, I restarted WAMP and it still done the same thing so I restarted my PC and it works perfect now, it seems that it stopped parsing while loops or something strange eh? Quote Link to comment https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/#findComment-1096355 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.