1

So basically I have the following loop to iterate database rows:

while($row = $mysql->fetch_assoc())

But I need to access the rows before this loop as well. So, I do this:

$inside = $mysql->fetch_assoc()

and $mysql loses its rows. When it gets to the while loop it simply does not enter it as the condition becomes NULL.

I tried the following

while($row = $inside)

but this just waits until timeout (loops indefinitely).

Any idea on how I could perform this, making up for the requirements above? Thank you very much for your help...

Fane
  • 1,978
  • 8
  • 30
  • 58
  • You could set the row to an array in the while, then access that array later when you need the rows for the second time.. – chris85 Jul 09 '16 at 20:16

2 Answers2

4

After you do this :

while ( $row = $mysql->fetch_assoc() )

The internal pointer of $resul is at the end. So you can move it to the beginning again :

$mysql->data_seek( 0 ); 
while ( $row = $mysql->fetch_assoc() )

All the rows are available again.

0

Use $mysql->data_seek(0) before to reset the row counter on your second loop iteration. It will allow you to loop through the rows again. See this helpful post for an example.

Blue
  • 22,608
  • 7
  • 62
  • 92