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...

1
  • 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.. Commented Jul 9, 2016 at 20:16

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.