Page 1 of 1

infinite loop

Posted: Fri Nov 16, 2007 11:56 pm
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;
  }

Posted: Sat Nov 17, 2007 12:01 am
by John Cartwright
Because your query will continuously execute over and over, create a new result set over and over, making you loop infinitely.

Posted: Sat Nov 17, 2007 12:28 am
by shivam0101
Thanks Jcart.

How to modify the code to work correctly?

Posted: Sat Nov 17, 2007 12:32 am
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.