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 trialSven Harder
14,943 PointsIntegrating PHP with Databases: Add genres to $item coding challenge
Hi, I have no idea how to solve the code challenge. See my code below.
Thank you
<?php
include "helper.php";
/*
* helper contains the following variables:
* $item is an array that contains details about the library item
* $results is a PDOstatement object with our genre results.
*/
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$item["genres"][] = $row;
}
2 Answers
sase habu
1,072 PointsUse the following code it should work....
// use while loop as you do.. while($row = $results->fetch(PDO::FETCH_ASSOC)) { // genres array with genre_id Key and genre value added to it. $item["genres"][$row["genre_id"]] = $row["genre"]; }
Sven Harder
14,943 PointsThank you
Mark Miller
45,831 PointsMark Miller
45,831 PointsShould there be only one key and one value for each row in the associative array? Why are two keys returned for each row?