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

Alfredo Prince
Alfredo Prince
6,175 Points

Is this in the right format? why is it throwing this error

PHP Fatal error: Uncaught Error: Undefined class constant 'FETCHASSOC' in /workdir/index.php:5 Stack trace: #0 {main} thrown in /workdir/index.php on line 5 Fatal error: Uncaught Error: Undefined class constant 'FETCHASSOC' in /workdir/index.php:5 Stack trace: #0 {main} thrown in /workdir/index.php on line 5

index.php
<?php

include "helper.php";

while($item = $results->fetch(PDO::FETCHASSOC)){
  $item[$results["genre"]][] =  $results["genre_id"];
}

/*
 * 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.
 */

4 Answers

Carl Whedbee
Carl Whedbee
18,577 Points

Here's your code: while($item = $results->fetch(PDO::FETCHASSOC)){ $item[$results["genre"]][] = $results["genre_id"]; }

Here's my code: while($row = $results->fetch(PDO::FETCH_ASSOC)) { $item["genres"][$row["genre_id"]] = $row["genre"]; }

$item is an array. $row is also an array. Notice the difference in getting and setting the values.

Alfredo Prince
Alfredo Prince
6,175 Points

from the question, I didn't know I had to take into account an arrow for $row. Why??

Alfredo Prince
Alfredo Prince
6,175 Points

Yea, that's actually the first thing i checked and I'm still sure what I'm doing wrong.

Carl Whedbee
Carl Whedbee
18,577 Points

Check your fetch_style ... try (PDO::FETCH_ASSOC) or loading your results into a different variable. $item is already declared as an array. Also take a look on how to get values from an array.

Alfredo Prince
Alfredo Prince
6,175 Points

That definitely got me a lot closer. It supposed to: $item is an array that contains details about the media item. $results is a PDOstatement object with our genre results. Loop through the $results and add an additional "genres" element to $item. This "genres" element should have an internal associative array with the key of genre_id and the value of genre.

I changed it around some since the results is the PDstatement object.

<?php

include "helper.php"; while($results = $item->fetch(PDO::FETCH_ASSOC)){ $results[$item["genre"]][] = $item["genre_id"]; }

but i still get an error PHP Fatal error: Uncaught Error: Call to a member function fetch() on array in /workdir/index.php:4 Stack trace: #0 {main} thrown in /workdir/index.php on line 4 Fatal error: Uncaught Error: Call to a member function fetch() on array in /workdir/index.php:4 Stack trace: #0 {main} thrown in /workdir/index.php on line 4

Check the docs