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

William Bohn
William Bohn
13,737 Points

Last Challenge in Integrating PHP with Databases- help!

I coded: While ($row = $results->fetch(PDO::FETCH_ASSOC)) { $item[$row["genre_id"]] []=$row["genre"]; }

and it keeps telling me nothing is added to the $item["genres"] array


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[$row["genre_id"]] []=$row["genre"];


1 Answer

Robert Kulagowski
Robert Kulagowski
4,954 Points

You don't have an associative array where the key is "genres" - your associative array is using a key that's tied to the value of genre_id.

After the loop terminates, what happens if you put in


Lets say that genre_id is numeric. You're creating an array where the index is going to be a number:

genre_id, genre
2,Science Fiction

Using your existing code:

$item[1] will be "Horror"

If you want an associative array, where you can do something like

$myGenreID = $item["Horror"];

Then the key for $item on the left-hand-side would need to be $row["genre"] and not the genre_id