infinite loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

infinite loop

Post by shivam0101 »

Why am i getting infinite loop when i execute below code?

Code: Select all

$sql_oproposals = "SELECT * FROM " . $this->tbl_suggestmatches . " WHERE report_card_status = 'No' AND ClientID = " . $client_id;
	   
	   
	   
	  // $total_oproposals = $this->GetNumRows($sql_oproposals);
	   while ($mysql_array = $this->FetchAll($sql_oproposals)) { 
	      echo $mysql_array['id'];
	   }


//code in class file

Code: Select all

function FetchAll($sql_query)
   {
       $num_rows_res=mysql_query($sql_query);
	   
	   $mysql_array = mysql_fetch_assoc($num_rows_res); 
	   
	   return $mysql_array;
  }
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Because your query will continuously execute over and over, create a new result set over and over, making you loop infinitely.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

Thanks Jcart.

How to modify the code to work correctly?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm not going to write the code for you ;) Wheres the learning experience in that?

Something you need to consider:
- You only want to execute the query once
- Seperate your FetchAll() into two methods, one to perform the query and one to fetch the result.

Once you've done that, then it should work as you expect.
Post Reply