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 trialJoe Scotto
8,282 PointsWhat exactly is $db->setAttribute() doing?
What exactly is an Attribute and what is it doing?
4 Answers
Joe Scotto
8,282 PointsI posted a question on reddit and got this response from a user named: Jake_Jeremy
Basically, PDO is a kind of library that wraps around database connections, making them easy to use. You can configure how those databases are supposed to work by setting attributes on them - thus changing the behaviour in comparison to what it was before.
For example, the line you posted would set the attribute "errmode" (communicated as an integer saved in the constant ATTR_ERRMODE in the class PDO) to "errmode_exception" (again, an integer constant in the class PDO). This means that any errors will be thrown as exceptions.
There are many different attributes you can set, and some are actually important to set - for example you should always set PDO::ATTR_EMULATE_PREPARES to false to ensure that native prepared statements are used if possible.
Essentially what he's saying is that it is setting a "configuration" of sorts of how the PDO class will interact with the data.
Brian Juhl
4,502 PointsWas just about to ask this. Thanks Joe Scotto!
jlampstack
23,932 PointsThanks for the explanation @Joe Scotto . From what I understand in simple terms, the attribute changes the behaviour of how errors are handled? But I still find this confusing, mainly because I see no difference at all in the error messages whether or not I use the attribute?
jlampstack
23,932 PointsI found this youtube video helpful as well..... for those who are more visual https://www.youtube.com/watch?v=PQ6PffoFRRY It's explained around the 5:30 mark of the video.