Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Does the while loop pull two separate strings from the row and fullname fields or an array?

As shown in the video, I was confused about what kind of data type the while loop pulls from the database. The video suggest the data is pullied as '$row: 'actor', 'John Livingston' Is this supposed to represent one $row variable with an array as a value? I'm not sure) so I reviewed some of the questions under the video to see if anyone else had the same problem understanding this.

The highlighted best answer from Zach Billingsley to a question headed as 'Confused. How does this code work?' dated 2017 suggests that the while loop pulls the data as two strings like this $row['role'] = 'actor' and then $row['fullname'] = 'Ron Livingston' and not as a single array?

Was this answer correct or is the r$row = $result->fetch(PDO::FETCH_ASSOC actually pulling just a single array and (for example) assigning as $row = ('actor','Ron Livingston', ) ?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Matt Nickolls! The $row is an associative array. It contains a "role" key and a "fullName" key. So the $row for say Keanu Reeves would look like:

$row = array("role" => "star", "fullName" = "Keanu Reeves";

Then we take the role and create yet another associative array for $item. So $item now might have something like:

array("title" => "Speed", "producer" => ["Jan De Bont"], "star" => ["Keanu Reeves", "Sandra Bullock"]);

The value at the "role" key is in itself an array and you are appending to it each time you hit a new person with the same role.

Hope this helps! :sparkles: