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 Querying Multiple Tables with JOIN

Mark Miller
Mark Miller
45,831 Points

Why 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.

Hey Mark,

If you are just getting a single record then fetch() is a little cleaner than fetchAll(). fetchAll() will still work, but it will just return a collection with a single item.

Thanks,

Ben

2 Answers

Victoria Loisi
Victoria Loisi
7,070 Points

If 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.

The 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));