php教程

PDO::setAttribute

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

PDO::setAttribute 设置属性

说明

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

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

  • 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

User Contributed Notes

rob51 at mac dot com 07-Nov-2018 03:10
Where would I find the default values of attributes?
yeboahnanaosei at gmail dot com 22-Dec-2017 04:42
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());
}
?>
vstoykov at proab dot info 19-Dec-2017 02:29
I am using PHP 5.6 and MySQL 5.0 on GoDaddy.
When executing a query like this:
<?php
    $stmt
= $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:
<?php
    $this
->PDO->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
?>
the query was executed successfully.
guillaume at thug dot com 09-Nov-2017 03:07
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 );          
      $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

  } catch (PDOException $e) {

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

  }

  return $pdo;
}
yeboahnanaosei at gmail dot com 11-Aug-2017 11:36
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. :)
steve at websmithery dot co dot uk 04-Jul-2017 07:21
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.
justinasu at gmail dot com 22-Dec-2015 06:51
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.
antoyo 11-Jan-2011 05:49
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);
?>
colinganderson [at] gmail [dot] com 18-May-2007 07:59
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
gregory dot szorc at gmail dot com 14-Feb-2007 08:32
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
m dot leuffen at gmx dot de 25-Jul-2006 06:44
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

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788