We have briefly covered setting connection attributes in Chapter 3 when we saw how to use exceptions as a means of error reporting. Connection attributes allow us to control certain aspects of the connection as well as to query such things as the driver name and version.
One way is to specify an array of attribute name/value pairs in the PDO constructor.
Another way is to call the
PDO::setAttribute()
method, which accepts two parameters:The attribute's name
The attribute's value
In PDO, attributes and their values are defined as constants in the PDO
class as in the following call in the common.inc.php
file:
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
It includes two such constants—PDO::ATTR_ERRMODE and PDO::ERRMODE_EXCEPTION
.
To get the value of an attribute, there is the PDO::getAttribute()
method. It accepts a single parameter, the attribute name, and returns the value of the attribute. For example, the following code would print...