Ryan
Ryan

Reputation: 787

Confused about how PHP for loop works

I have a for loop that displays the first six items in a MySQL database. I then breakout to another piece of code, after which I have another for loop that displays the next six items in my database.

Though this is exactly the functionality that I wanted, I am confused as to how it works, and was hoping somebody could clarify for me. How does the second for loop know to start off where the first one left off?

My code is as follows:

for ($i = 1; $i <= 6; $i++) {
  $row = mysqli_fetch_array($result);

  $id = $row['id'];
  $title = $row['title'];
  $file = $row['file'];

  // Display items

}

The second for loop is exactly the same.

Upvotes: 1

Views: 74

Answers (1)

Sethunath K M
Sethunath K M

Reputation: 4761

There is a resultset pointer that is created when you execute a query that returns a result set . Each call to mysqli_fetch_array advances the resultset pointer to next record

When you are at the start of second for loop the result set pointer is pointing the 7th record

Upvotes: 6

Related Questions