文字

PDO::setAttribute

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

PDO::setAttribute 设置属性

说明

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

设置数据库句柄属性。下面列出了一些可用的通用属性;有些驱动可能使用另外的特定属性。

  • PDO::ATTR_CASE:强制列名为指定的大小写。

    • PDO::CASE_LOWER:强制列名小写。

    • PDO::CASE_NATURAL:保留数据库驱动返回的列名。

    • PDO::CASE_UPPER:强制列名大写。

  • PDO::ATTR_ERRMODE:错误报告。

    • PDO::ERRMODE_SILENT: 仅设置错误代码。

    • PDO::ERRMODE_WARNING: 引发 E_WARNING 错误

    • PDO::ERRMODE_EXCEPTION: 抛出 exceptions 异常。

  • PDO::ATTR_ORACLE_NULLS (在所有驱动中都可用,不仅限于Oracle): 转换 NULL 和空字符串。

    • PDO::NULL_NATURAL: 不转换。

    • PDO::NULL_EMPTY_STRING: 将空字符串转换成 NULL

    • PDO::NULL_TO_STRING: 将 NULL 转换成空字符串。

  • PDO::ATTR_STRINGIFY_FETCHES: 提取的时候将数值转换为字符串。 Requires bool .

  • PDO::ATTR_STATEMENT_CLASS: 设置从PDOStatement派生的用户提供的语句类。 不能用于持久的PDO实例。 需要 array(string 类名, array(mixed 构造函数的参数))

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

  • PDO::ATTR_AUTOCOMMIT (在OCI,Firebird 以及 MySQL中可用): 是否自动提交每个单独的语句。

  • PDO::ATTR_EMULATE_PREPARES 启用或禁用预处理语句的模拟。 有些驱动不支持或有限度地支持本地预处理。使用此设置强制PDO总是模拟预处理语句(如果为 TRUE ),或试着使用本地预处理语句(如果为 FALSE )。如果驱动不能成功预处理当前查询,它将总是回到模拟预处理语句上。 需要 bool 类型。

  • PDO::MYSQL_ATTR_USE_BUFFERED_QUERY (在MySQL中可用): 使用缓冲查询。

  • PDO::ATTR_DEFAULT_FETCH_MODE: 设置默认的提取模式。关于模式的说明可以在 PDOStatement::fetch() 文档找到。

返回值

成功时返回 TRUE , 或者在失败时返回 FALSE

用户评论:

[#1] antoyo [2011-01-11 17:49:30]

There is also a way to specifie the default fetch mode :
<?php
$connection 
= new PDO($connection_string);
$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_OBJ);
?>

[#2] colinganderson [at] gmail [dot] com [2007-05-18 07:59:44]

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_CASEPDO::CASE_NATURAL);
?>


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

.Colin

[#3] gregory dot szorc at gmail dot com [2007-02-14 20:32:38]

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

[#4] m dot leuffen at gmx dot de [2006-07-25 18:44:04]

Hi,

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
(PDO::MYSQL_ATTR_MAX_BUFFER_SIZE=>1024*1024*50));

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

Bye,
  Matthias

上一篇: 下一篇: