(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)

PDO::setAttribute Set an attribute


public PDO::setAttribute(int $attribute, mixed $value): bool

Sets an attribute on the database handle. Some available generic attributes are listed below; some drivers may make use of additional driver specific attributes. Note that driver specific attributes must not be used with other drivers.


Force column names to a specific case. Can take one of the following values:

Force column names to lower case.
Leave column names as returned by the database driver.
Force column names to upper case.

Error reporting mode of PDO. Can take one of the following values:

Only sets error codes.
Raises E_WARNING diagnostics.
Throws PDOExceptions.

Note: This attribute is available with all drivers, not just Oracle.

Determines if and how null and empty strings should be converted. Can take one of the following values:

No conversion takes place.
Empty strings get converted to null.
null gets converted to an empty string.

Whether to convert numeric values to strings when fetching. Takes a value of type bool: true to enable and false to disable.


Set user-supplied statement class derived from PDOStatement. Requires array(string classname, array(mixed constructor_args)).


Cannot be used with persistent PDO instances.


Specifies the timeout duration in seconds. Takes a value of type int.


Not all drivers support this option, and its meaning may differ from driver to driver. For example, SQLite will wait for up to this time value before giving up on obtaining a writable lock, but other drivers may interpret this as a connection or a read timeout interval.


Note: Only available for the OCI, Firebird, and MySQL drivers.

Whether to autocommit every single statement. Takes a value of type bool: true to enable and false to disable. By default, true.


Note: Only available for the OCI, Firebird, and MySQL drivers.

Whether enable or disable emulation of prepared statements. Some drivers do not support prepared statements natively or have limited support for them. If set to true PDO will always emulate prepared statements, otherwise PDO will attempt to use native prepared statements. In case the driver cannot successfully prepare the current query, PDO will always fall back to emulating the prepared statement.


Note: Only available for the MySQL driver.

Whether to use buffered queries. Takes a value of type bool: true to enable and false to disable. By default, true.


Set the default fetch mode. A description of the modes and how to use them is available in the PDOStatement::fetch() documentation.



The attribute to modify.


The value to set the attribute, might require a specific type depending on the attribute.

Return Values

Returns true on success or false on failure.

See Also

add a note add a note

User Contributed Notes 11 notes

colinganderson [at] gmail [dot] com
16 years ago
Because no examples are provided, and to alleviate any confusion as a result, the setAttribute() method is invoked like so:

setAttribute(ATTRIBUTE, OPTION);

So, if I wanted to ensure that the column names returned from a query were returned in the case the database driver returned them (rather than having them returned in all upper case [as is the default on some of the PDO extensions]), I would do the following:

// Create a new database connection.
$dbConnection = new PDO($dsn, $user, $pass);

// Set the case in which to return column_names.
$dbConnection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);

Hope this helps some of you who learn by example (as is the case with me).

yeboahnanaosei at gmail dot com
5 years ago
This is an update to a note I wrote earlier concerning how to set multiple attributes when you create you PDO connection string.

You can put all the attributes you want in an associative array and pass that array as the fourth parameter in your connection string. So it goes like this:
= [

// Now you create your connection string
try {
// Then pass the options as the last parameter in the connection string
$connection = new PDO("mysql:host=$host; dbname=$dbname", $user, $password, $options);

// That's how you can set multiple attributes
} catch(PDOException $e) {
"Database connection failed: " . $e->getMessage());
yeboahnanaosei at gmail dot com
6 years ago
Well, I have not seen it mentioned anywhere and thought its worth mentioning. It might help someone. If you are wondering whether you can set multiple attributes then the answer is yes.

You can do it like this:
try {
    $connection = new PDO("mysql:host=$host; dbname=$dbname", $user, $password);
    // You can begin setting all the attributes you want.
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
    $connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);

    // That's how you can set multiple attributes
catch(PDOException $e)
    die("Database connection failed: " . $e->getMessage());

I hope this helps somebody. :)
gregory dot szorc at gmail dot com
16 years ago
It is worth noting that not all attributes may be settable via setAttribute().  For example, PDO::MYSQL_ATTR_MAX_BUFFER_SIZE is only settable in PDO::__construct().  You must pass PDO::MYSQL_ATTR_MAX_BUFFER_SIZE as part of the optional 4th parameter to the constructor.  This is detailed in http://bugs.php.net/bug.php?id=38015
12 years ago
There is also a way to specifie the default fetch mode :
= new PDO($connection_string);
$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
steve at websmithery dot co dot uk
6 years ago
For PDO::ATTR_EMULATE_PREPARES, the manual states a boolean value is required. However, when getAttribute() is used to check this value, an integer (1 or 0) is returned rather than true or false.

This means that if you are checking a PDO object is configured as required then

// Check emulate prepares is off
if ($pdo->getAttribute(\PDO::ATTR_EMULATE_PREPARES) !== false) {
/* do something */

will always 'do something', regardless.


// Check emulate prepares is off
if ($pdo->getAttribute(\PDO::ATTR_EMULATE_PREPARES) != false) {
/* do something */


// Check emulate prepares is off
if ($pdo->getAttribute(\PDO::ATTR_EMULATE_PREPARES) !== 0) {
/* do something */

is needed instead.

Also worth noting that setAttribute() does, in fact, accept an integer value if you want to be consistent.
rob51 at mac dot com
5 years ago
Where would I find the default values of attributes?
justinasu at gmail dot com
7 years ago
in v5.5 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY can only be set in PDO constructor, not by passing it into setAttribute.
If you set it with setAttribute it will not work. getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY) will return 0.
guillaume at thug dot com
6 years ago
function pdo_connect(){
  try {

      $pdo = new PDO('mysql:host=localhost;dbname='.DB_NAME, DB_USER, DB_PASS);
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
      $pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );          

  } catch (PDOException $e) {

      die("Error!: " . $e->getMessage() . "<br/>");


  return $pdo;
m dot leuffen at gmx dot de
17 years ago

if you are wondering about a size-bound (1 MB) on blob and text fields after upgrading to PHP5.1.4. You might try to increase this limit by using the setAttribute() method.

This will fail. Instead use the options array when instantiating the pdo:

$pdo = new PDO ("connection_settings", "user", "pass", array

This should fix the problem and increase the limit to 50 MB.

vstoykov at proab dot info
5 years ago
I am using PHP 5.6 and MySQL 5.0 on GoDaddy.
When executing a query like this:
= $this->PDO->query("SHOW CREATE TABLE table");
I get:
Uncaught exception 'PDOException' with message
'SQLSTATE[HY000]: General error: 2030 This command is not supported in the prepared statement protocol yet'
After I added:
->PDO->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
the query was executed successfully.
To Top