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 Many Relationships

Sven Harder
Sven Harder
14,943 Points

Integrating 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

index.php
<?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
sase habu
1,072 Points

Use 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"]; }

Mark Miller
Mark Miller
45,831 Points

Should there be only one key and one value for each row in the associative array? Why are two keys returned for each row?