Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Integrating PHP with Databases Using Relational Tables Fetching in a While Loop

How while loop is incrementing here?

Referring to below code:

while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
    $item[$row["role"]][] = $row["fullname"];
}

I understand the concept but if while loop is incrementing by itself, what is the differnce between foreach and while? Why are we not using foreach instead?

3 Answers

The while loop continues infinitely so long as the statement that it is looping over evaluates to true. So, in this case it's going to fetch each row one at a time until the query fails for no additional rows. It's generally a better practice to use foreach when you can because you can end up in an infinite while loop so long as what you're evaluating is true.

Your application doesn't really know what's in the database until it's retrieved so a loop with a finite end makes less sense.

Mark Miller
Mark Miller
45,831 Points

But why does the value change each time through the while loop? There seems to be nothing causing the retrieval of the next thing. The while loop should be stuck with the first one fetched, and then it should just repeat that over and over again. I don't see why a new value would ever be retrieved since we're just repeating the same fetch without incrementing anything forward. Does our database object have something in it that causes the forward incrementation? Apparently so, but why? The same code is running each time through the loop, why would a new value be retrieved?

The value changes each time we call $results->fetch because there is a previous block of code that isn't shown that actually pulls the results. We're not actually making additional database calls and are instead using PDO to access that information. The fetch method pulls the next row from our results and assigns it to $row which we then act upon until there are no additional rows and then returns FALSE. When there are no more rows to pull which exits out of the while loop. The same could be accomplished using $rows = $results->fetchAll and a foreach loop over $rows.

TLDR - We aren't hitting the database in the while loop; that happened previously and are instead using while + PDO to loop through the results.

Edit: Sorry for the late response to this, I hadn't seen it previously because I haven't been on Treehouse in a little while.

Mark Miller
Mark Miller
45,831 Points

Thanks for your help clarifying this. I still want to see how this fetch() method is moving forward and remembering what it did last time it was called. That seems to be where the loop is. I don't know if we need to nest that fetch() loop inside of a while() loop. We may know the size of the result set, so we don't need to use while() in that case. I would rather use fetchAll() and then foreach() so I know what I'm using.

A foreach() loop will cause an infinite loop unless an end is specified. A while() loop will only loop while a condition is true.

Example. For we don't know how many stars each movie has. Forest Gump may have 6 stars, another movie may have 4 stars etc. For this reason we can't set an endpoint for a forloop().

A while loop is best used when we don't know the number of times we need to loop, where it will end. It will keep looping until the condition is false.