farban6 Posted June 11, 2011 Share Posted June 11, 2011 hello there making a logging ip system and for some reason its stuck in a infinate loop, always looping the first result in the query <?php include "config.php"; class Database_Connection { private $connection; public function __construct() { $this->connection = mysql_connect(HOST,USERNAME,PASSWORD); $this->connect_to_DB(); } public function connect_to_DB() { return $this->connection; if (!$this->connection) { die('Could not connect: ' . mysql_error()); } } } class Database_Select { private $database; public function __construct() { $link = new Database_Connection(); $this->database = mysql_select_db(DATABASE, $link->connect_to_DB()); if (!$this->database) { die('Could not use database: ' . mysql_error()); } } } class IP_Log { private $ip; public function __construct() { $this->ip = $_SERVER['REMOTE_ADDR']; } public function get_ip() { return $this->ip; } } class Insert_IP { public function __construct() { $this->insert_ip(); } public function insert_ip() { $iplog = new IP_Log(); mysql_query("INSERT INTO `ip_logger`.`ip_logger` (`log_ID`, `log_IP`, `log_date`) VALUES (NULL, '".$iplog->get_ip()."', CURRENT_TIMESTAMP)"); } } class Grab_IP { public function __construct() { $this->grab_ip(); } public function grab_ip() { return mysql_query("SELECT * FROM `ip_logger`"); } } class Show_IP { public function __construct() { $this->show_ip(); } public function show_ip() { $result = new Grab_IP(); while($row = mysql_fetch_array($result->grab_ip(), MYSQL_BOTH)){ echo $row['log_ID']. " - ". $row['log_IP']. $row['log_date']; echo "<br />"; } } } $database = new Database_Select(); $insert = new Insert_IP(); $show = new Show_IP(); ?> Link to comment https://forums.phpfreaks.com/topic/239086-why-is-it-in-a-infinate-loop/ Share on other sites More sharing options...
Psycho Posted June 11, 2011 Share Posted June 11, 2011 Well, you only have one loop in that code while($row = mysql_fetch_array($result->grab_ip(), MYSQL_BOTH)){ On each iteration of the loop you are calling$result->grab_ip(). So it is rerunning the query on each iteration of the loop - so it never ends. You need to call grab_ip() ONE TIME and THEN run the loop on that result. Also, why have the overhead of using mysql_fetch_array() to return both associative and numerically indexed values when you are only usign the associative values? $ipResult = $result->grab_ip(); while($row = mysql_fetch_assoc($ipResult)){ Link to comment https://forums.phpfreaks.com/topic/239086-why-is-it-in-a-infinate-loop/#findComment-1228397 Share on other sites More sharing options...
farban6 Posted June 11, 2011 Author Share Posted June 11, 2011 It works ! and its lazyness im afraid, although I was going to change it when I finished the system. How did making a varible of the instance fix the problem, to my eyes it just looks the same as what I put in. Link to comment https://forums.phpfreaks.com/topic/239086-why-is-it-in-a-infinate-loop/#findComment-1228399 Share on other sites More sharing options...
Psycho Posted June 11, 2011 Share Posted June 11, 2011 How did making a varible of the instance fix the problem, to my eyes it just looks the same as what I put in. I already explained that. By putting the function call in the while() loop you are calling the grab_ip() function on each iteration of the loop. So, instead of iterating through the results of ONE call to the grab_ip() function you are only processing the first record, getting a new set of records, processing the first record, getting a new set of records, etc., etc. Link to comment https://forums.phpfreaks.com/topic/239086-why-is-it-in-a-infinate-loop/#findComment-1228422 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.