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 trialAlfredo Prince
6,175 PointsIs 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
<?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
18,577 PointsHere'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
6,175 PointsYea, that's actually the first thing i checked and I'm still sure what I'm doing wrong.
Carl Whedbee
18,577 PointsCheck 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
6,175 PointsThat 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
Joshua Elmore
15,250 PointsCheck the docs
Alfredo Prince
6,175 PointsAlfredo Prince
6,175 Pointsfrom the question, I didn't know I had to take into account an arrow for $row. Why??