PHP 8.4.1 Released!

PDO::setAttribute

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

PDO::setAttribute 设置属性

说明

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

设置数据库句柄属性。下面列出了一些可用的通用属性;有些驱动可能使用另外的特定属性。 请注意,特定的驱动程序属性不得用于其他驱动程序。

PDO::ATTR_CASE

强制列名为指定的大小写。可以采用以下某个值:

PDO::CASE_LOWER
强制列名小写。
PDO::CASE_NATURAL
保留数据库驱动返回的列名。
PDO::CASE_UPPER
强制列名大写。
PDO::ATTR_ERRMODE

PDO 的报错方式。可以采用以下某个值:

PDO::ERRMODE_SILENT
仅设置错误代码。
PDO::ERRMODE_WARNING
引发 E_WARNING 警告。
PDO::ERRMODE_EXCEPTION
抛出 PDOException
PDO::ATTR_ORACLE_NULLS

注意: 此属性适用于所有驱动程序,而不仅仅是 Oracle。

确定是否以及如何转换 null 和空字符串。可以采用以下值之一:

PDO::NULL_NATURAL
不发生转换。
PDO::NULL_EMPTY_STRING
空字符串转换为 null
PDO::NULL_TO_STRING
null 被转换为空字符串。
PDO::ATTR_STRINGIFY_FETCHES

获取时是否将数值转换为字符串。值采用 bool 类型:true 表示启用,false 表示禁用。

PDO::ATTR_STATEMENT_CLASS

设置从 PDOStatement 派生的用户提供的语句类。 需要 array(string classname, array(mixed constructor_args))

警告

不能用于持久 PDO 实例。

PDO::ATTR_TIMEOUT

指定超时的秒数。需要 int 类型的值。

注意:

并非所有驱动都支持此选项,这意味着驱动和驱动之间可能会有差异。比如,SQLite 等待的时间达到此值后就放弃获取可写锁,但其他驱动可能会将此值解释为一个连接或读取超时的间隔。

PDO::ATTR_AUTOCOMMIT

注意: 仅适用于 OCI、Firebird 和 MySQL 驱动程序。

是否自动提交每个单独的语句。采用 bool 类型的值:true 表示启用,false 表示禁用。默认为 true

PDO::ATTR_EMULATE_PREPARES

注意: 仅适用于 OCI、Firebird 和 MySQL 驱动程序。

是否启用或禁用预处理语句的模拟。有些驱动天然不支持或有限度地支持预处理语句。如果设置为 true,PDO 始终模拟预处理语句,否则 PDO 将会尝试使用本地预处理语句。如果驱动不能成功预处理当前查询,PDO 将始终回退到模拟预处理语句上。

PDO::MYSQL_ATTR_USE_BUFFERED_QUERY

注意: 仅适用于 MySQL 驱动程序。

是否使用缓冲查询。采用 bool 类型的值:true 表示启用,false 表示禁用。默认情况下,默认为 true

PDO::ATTR_DEFAULT_FETCH_MODE

设置默认获取模式。关于模式的说明以及如何使用可以在 PDOStatement::fetch() 文档找到。

参数

attribute

要修改的属性。

value

设置 attribute 的值,属性的不同导致需要的类型也会不同。

返回值

成功时返回 true, 或者在失败时返回 false

参见

添加备注

用户贡献的备注 8 notes

up
124
colinganderson [at] gmail [dot] com
17 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:

<?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
up
31
yeboahnanaosei at gmail dot com
6 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:
<?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 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) {
die(
"Database connection failed: " . $e->getMessage());
}
?>
up
18
gregory dot szorc at gmail dot com
17 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
up
15
yeboahnanaosei at gmail dot com
7 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. :)
up
1
Anonymous
3 years ago
Note that contrary to most PDO methods, setAttribute does not throw a PDOException when it returns false.
up
5
steve at websmithery dot co dot uk
7 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

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

will always 'do something', regardless.

Either

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

or

<?php
// 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.
up
11
antoyo
13 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);
?>
up
-1
david at datava dot com
7 months 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