Welcome Guest, Not a member yet? Register   Sign In
CI_DB_mysql_result Object causing infinite loop
#1

[eluser]Unknown[/eluser]
Hey folks, doing a simple Active Record select. It looks a little something like this:

Code:
$query = $CI->db->select( 'answer' )
                      ->where( 'qID', $xid )
                      ->get( 'qAnswer' );

$query becomes the expected CI_DB_mysql_result Object with result rows and a current row of 0. Expected.

Code:
while($row = $query->row() )
{
       print_r( $row );
       // some actual code
      }

This is a commonly used pattern - but this time it triggers an infinite loop. When I output the row, it turns out that $query is dumping the first initial row infinitely many times. It is never iterating.

https://github.com/EllisLab/CodeIgniter/issues/2298 seems related but does not lead to a solution. Ideas?

Edit: I put this in Libraries because this code is in a custom library class.
#2

[eluser]CroNiX[/eluser]
Code:
$query = $CI->db->select( 'answer' )
  ->where( 'qID', $xid )
  ->get( 'qAnswer' )
  ->result_array();

Code:
foreach($query as $row)
{
  print_r($row);
}
#3

[eluser]Unknown[/eluser]
I went with a similar approach,
Code:
foreach( $query->result() as $row)
and it's working for now. Why is this needed? I used the other pattern 3 or 4 times just above with no problems? Is this a framework bug?




Theme © iAndrew 2016 - Forum software by © MyBB