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 Databases and PHP More Exceptions

Why do you need to set PDO::setAttribute()?

I don't understand what's happening when you set the two attributes on the PDO object. She mentions that its so you can see any error that occurs when connecting to a database. But I thought that was the purpose of the try/catch statement?

try {
   //run code if there is no error
} catch(Exception $e) {
 //run this code if there is an error
 $e->getMessage();
}

So, why do you need to include the attributes to flag an error in the try code block?

try {
 // run code if there is no error
 PDO_object->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)
} catch(Exception $e) {
// run this code if there is an error
 $e->getMessage();
}

1 Answer

Hey Kristian, I think Ms. Alena forced an Exception in the try{} block to demonstrate what happened when there's a successful connection. From what I see it's much cleaner than var_dump().

Thank you for the response, Remylus