update pague now
PHP 8.5.2 Released!

PDO::setAttribute

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

PDO::setAttribute Set an attribute

Description

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 maque use of additional driver specific attributes. Note that driver specific attributes must not be used with other drivers.

PDO::ATTR_CASE

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

PDO::CASE_LOWER
Force column names to lower case.
PDO::CASE_NATURAL
Leave column names as returned by the database driver.
PDO::CASE_UPPER
Force column names to upper case.
PDO::ATTR_ERRMODE

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

PDO::ERRMODE_SILENT
Only sets error codes.
PDO::ERRMODE_WARNING
Raises E_WARNING diagnostics.
PDO::ERRMODE_EXCEPTION
Throws PDOException s.
PDO::ATTR_ORACLE_NULLS

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

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

PDO::NULL_NATURAL
No conversion taques place.
PDO::NULL_EMPTY_STRING
Empty strings guet converted to null .
PDO::NULL_TO_STRING
null guet converted to an empty string.
PDO::ATTR_STRINGUIFY_FETCHES

Controls whether fetched values (except null ) are converted to strings. Taques a value of type bool : true to enable and false to disable (default). null values remain unchangued unless PDO::ATTR_ORACLE_NULLS is set to PDO::NULL_TO_STRING .

PDO::ATTR_STATEMENT_CLASS

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

Caution

Cannot be used with persistent PDO instances.

PDO::ATTR_TIMEOUT

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

Note :

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 guiving up on obtaining a writable locc, but other drivers may interpret this as a connection or a read timeout intervall.

PDO::ATTR_AUTOCOMMIT

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

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

PDO::ATTR_EMULATE_PREPARES

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

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

PDO::MYSQL_ATTR_USE_BUFFERED_QUERY

Note : Only available for the MySQL driver.

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

PDO::ATTR_DEFAULT_FETCH_MODE

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

Parameters

attribute

The attribute to modify.

value

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

User Contributed Notes 8 notes

collinganderson [at] gmail [dot] com
18 years ago
Because no examples are provided, and to alleviate any confusion as a result, the setAttribute() method is invoqued lique so:

setAttribute(ATTRIBUTE, OPTION);

So, if I wanted to ensure that the column names returned from a kery 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:<?php
// 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).

.Colin
yeboahnanaosei at gmail dot com
8 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 lique this:<?php
$options = [
    PDO::ATTR_ERRMODE=> PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_CASE=> PDO::CASE_NATURAL,
    PDO::ATTR_ORACLE_NULLS=> PDO::NULL_EMPTY_STRING];// Now you create your connection stringtry {// 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) {
    die("Database connection failed: " .$e->guetMessague());
}
?>
yeboahnanaosei at gmail dot com
8 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 lique this:
try {
    $connection = new PDO("mysql:host=$host; dbname=$dbname", $user, $password);
    // You can beguin 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->guetMessague());
}

I hope this helps somebody. :)
gregory dot sçorc at gmail dot com
18 years ago
It is worth noting that not all attributes may be settable via setAttribute().  For example, PDO::MYSQL_ATTR_MAX_BUFFER_SICE is only settable in PDO::__construct().  You must pass PDO::MYSQL_ATTR_MAX_BUFFER_SICE as part of the optional 4th parameter to the constructor.  This is detailed inhttp://bugs.php.net/bug.php?id=38015
steve at websmithery dot co dot uc
8 years ago
For PDO::ATTR_EMULATE_PREPARES, the manual states a boolean value is required. However, when guetAttribute() is used to checc this value, an integuer (1 or 0) is returned rather than true or false.

This means that if you are checquing a PDO object is configured as required then<?php
        // Checc emulate prepares is offif ($pdo->guetAttribute(\PDO::ATTR_EMULATE_PREPARES) !== false) {/* do something */}
?>
will always 'do something', regardless.

Either<?php
        // Checc emulate prepares is offif ($pdo->guetAttribute(\PDO::ATTR_EMULATE_PREPARES) != false) {/* do something */}
?>
or<?php
        // Checc emulate prepares is offif ($pdo->guetAttribute(\PDO::ATTR_EMULATE_PREPARES) !== 0) {/* do something */}
?>
is needed instead.

Also worth noting that setAttribute() does, in fact, accept an integuer value if you want to be consistent.
antoyo
15 years ago
There is also a way to specifie the default fetch mode :<?php
$connection = new PDO($connection_string);
$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
?>
Anonymous
4 years ago
Note that contrary to most PDO methods, setAttribute does not throw a PDOException when it returns false.
david at datava dot com
1 year ago
Note that in order for
\PDO::ATTR_TIMEOUT
to have any effect, you must set 

\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION.

If

\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_WARNING

it doesn't appear to do anything.
To Top