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 trialMark Miller
45,831 PointsWhy not fetchAll() when the query is specifically returning what we need?
I didn't understand why she changed fetchAll() to just fetch(). It seems that the query is doing its job of returning what we want, so we would want to fetch all of that.
2 Answers
Victoria Loisi
7,070 PointsIf you are going to get just one result, and you know that there's no chance of getting more than one, use fetch() so you don't need to loop through the results, because it's just one. To retrieve results using fetchAll() you always need to loop through them.
Dennis Amiel Domingo
17,813 PointsThe query was focused on 1 media item hence the function name "single_item_array()"
Also, she only did a var_dump of 1 $id item:
var_dump(single_item_array(1));
Ben Payne
1,464 PointsBen Payne
1,464 PointsHey Mark,
If you are just getting a single record then
fetch()
is a little cleaner thanfetchAll()
.fetchAll()
will still work, but it will just return a collection with a single item.Thanks,
Ben