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

Development Tools Using PHP with MySQL Using Relationship Tables in MySQL Fetching in a While Loop

SQL While loop help

This is the while loop randy uses in this video:

while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
  $product["sizes"][] = $row["size"];
}

I don't understand why it fetches a new row every time. When you call the fetch command, shouldn't it give you the first row of the table? So then it should enter the while loop, and then when it loops through a second time, why does it give the second row of the table? Nothing changes in the fetch statement, so it seems like it should be an infinite loop.

Also, can you explain what the $row["size"] command does? I'm a bit unclear about where the "size" key comes from.

Thanks!!