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 PHP & Databases with PDO PDO Queries & Results PDO Queries & Results

Having trouble with the Quiz Question

The Quiz Question that asks to complete the code: $results = $db->______($sql); is weird to me.

I'm not sure why this is not "fetchAll", and leaving without parentheses to have the ($sql) work as the parameter set.

1 Answer

Clare A
Clare A
23,994 Points

I'm new to this too but my understanding is....

'$db' in the above context is an instance of the PDO class rather than PDO statement class and fetchAll is not an available method on the PDO class.

First I'd need to get a result set from the database by calling the query method on my PDO, $db (this is what's happening in the quiz question). This saves the results as a PDO statement object ($results). Then, if I wanted to use this data, only then could I use fetchAll() but I'd need to do this on the PDO statement object which would then return it to me in an array.