Jump to content

Stuck In An Infinite Loop?


Guest FirefoxRocks

Recommended Posts

Guest FirefoxRocks

Somehow this is stuck in an infinite loop I think:

$users = new Uh();while ($staff = $users->myFunction()) {	echo $staff["id"];	if($staff["id"]==="1") {		echo "<br>";	}	 	echo $users->getUserById($staff["id"])->lastName;	echo $users->getUserById($staff["id"])->firstName;}

It doesn't matter if I use mysql_fetch_object, mysql_fetch_array or even mysql_fetch_row, it still freezes up and then the browser display's nothing.What is the problem? This is my first day of trying to use objects/classes.

Link to comment
Share on other sites

Guest FirefoxRocks
public function myFunction($order='lastName') {	$q = mysql_query("SELECT * FROM `users` WHERE `group`='something' or `group`='somethingelse' ORDER BY `$order`;");	return (is_resource($q)) ? mysql_fetch_assoc($q) : mysql_error();}

Link to comment
Share on other sites

Show us the entire Uh class.Edit: it also helps to name things logically :)

Link to comment
Share on other sites

Guest FirefoxRocks
Show us the entire Uh class.Edit: it also helps to name things logically :)
class Uh {				public function asdf($i, $o="id") {			return mysql_fetch_object(mysql_query("SELECT * FROM `users` WHERE `id`='$i' ORDER BY `$o`;"));		}		public function ghjk($qwe, $o="id") {			return mysql_fetch_object(mysql_query("SELECT * FROM `users` WHERE `username`='$qwe' ORDER BY `$o`;"));		}public function myFunction($order='lastName') {	$q = mysql_query("SELECT * FROM `users` WHERE `group`='something' or `group`='somethingelse' ORDER BY `$order`;");	return (is_resource($q)) ? mysql_fetch_assoc($q) : mysql_error();}	}

Link to comment
Share on other sites

I suppose by myFunction() you mean asdf() or ghjk()? (gah)In those two functions, you are querying the database over and over again, so every time you go through the resource is recreated and the first record is retrieved.

Link to comment
Share on other sites

Guest FirefoxRocks
I suppose by myFunction() you mean asdf() or ghjk()? (gah)In those two functions, you are querying the database over and over again, so every time you go through the resource is recreated and the first record is retrieved.
No, by myFunction I mean myFunction() as the first function that I posted here.
Link to comment
Share on other sites

The loop is only going to end if the condition evaluates to false. So if $staff is not false, the loop will not end. Your function doesn't return false, it either returns an array or an error message. If that query ever has an error, the loop will never end because it will just keep returning the error message over and over. Also, just look at what your function does. You run a query and return the first result. So all that loop is doing is you keep having it run the exact same query and then returning the exact same first result. You're just doing the same thing over and over again. Of course it's not going to end.

Link to comment
Share on other sites

Guest FirefoxRocks

I thought that using while returns each row that it finds using mysql_fetch_* until there are no more rows, in which it returns false.I also tried:

while ($staff = mysql_fetch_object($users->getStaff())) { ... }

and in myFunction() I returned mysql_query("..."); but that still has the same result (well I can't see any results, but it is also stuck in a loop.Also, asdf() and ghjk() work fine.

Link to comment
Share on other sites

I thought that using while returns each row
While just makes a loop. While doesn't return anything, it just keeps looping until the condition is false, and then it stops. It's up to you to make the condition false at some point in order to end the loop. When you do this:while ($staff = $users->myFunction()) {Every time through the loop it executes myFunction. Every time myFunction gets executed it does the same thing, it runs a query and returns the first result. So every time through the loop it's going to run the same query again, and return the first result. It's not returning the next result in the result set, it's just running the query and returning the first result. If you want to return the next result then you need to have the function save the result set and then use mysql_fetch_assoc or whatever to get the next row. It would make sense to have one function that runs the query and saves the result set in a private class member, and then another function that keeps getting the next result from that same member. e.g.:$users->getResults();while ($staff = $users->nextResult()) {Incidentally, this is the reason why names like 'myFunction', 'asdf', 'ghjk' etc aren't very useful. You'll look at that code in another year and not have any idea what it's doing.
Link to comment
Share on other sites

Guest FirefoxRocks

Ok I used this instead to move the internal data pointer.

for($i=0; $i<mysql_num_rows($staff); $i++) {	mysql_data_seek($staff, $i);	$sd = mysql_fetch_object($staff);	echo $sd->id;	echo $users->asdf($sd->id)->lastName;	echo $users->asdf($sd->id)->firstName;}

It seems to work fine because it is moving the internal data pointer and only running the block for the number of rows.And do you really think my functions are called asdf(), ghjk(), etc?

Link to comment
Share on other sites

public function myFunction($order='lastName') {	$q = mysql_query("SELECT * FROM `users` WHERE `group`='something' or `group`='somethingelse' ORDER BY `$order`;");	return (is_resource($q)) ? mysql_fetch_assoc($q) : mysql_error();}

If there is a mysql_error() the loop will be infinite, because a string evaluates to true and mysql_error() returns a string.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...