Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/210070-while-loop-just-keeps-on-running/
Share on other sites

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.

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

 

 

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!

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?

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

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.