PDO::setAttribute

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

PDO::setAttribute Set an attribute

Description

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

Sets an attribute on the database handle. Some of the available generic attributes are listed below; some drivers may make use of additional driver specific attributes.

  • PDO::ATTR_CASE: Force column names to a specific case.

    • 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.

    • PDO::ERRMODE_SILENT: Just set error codes.

    • PDO::ERRMODE_WARNING: Raise E_WARNING.

    • PDO::ERRMODE_EXCEPTION: Throw exceptions.

  • PDO::ATTR_ORACLE_NULLS (available with all drivers, not just Oracle): Conversion of NULL and empty strings.

    • PDO::NULL_NATURAL: No conversion.

    • PDO::NULL_EMPTY_STRING: Empty string is converted to NULL.

    • PDO::NULL_TO_STRING: NULL is converted to an empty string.

  • PDO::ATTR_STRINGIFY_FETCHES: Convert numeric values to strings when fetching. Requires bool.

  • PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).

  • PDO::ATTR_TIMEOUT: Specifies the timeout duration in seconds. Not all drivers support this option, and it's meaning may differ from driver to driver. For example, sqlite will wait for up to this time value before giving up on obtaining an writable lock, but other drivers may interpret this as a connect or a read timeout interval. Requires int.

  • PDO::ATTR_AUTOCOMMIT (available in OCI, Firebird and MySQL): Whether to autocommit every single statement.

  • PDO::ATTR_EMULATE_PREPARES Enables or disables emulation of prepared statements. Some drivers do not support native prepared statements or have limited support for them. Use this setting to force PDO to either always emulate prepared statements (if TRUE), or to try to use native prepared statements (if FALSE). It will always fall back to emulating the prepared statement if the driver cannot successfully prepare the current query. Requires bool.

  • PDO::MYSQL_ATTR_USE_BUFFERED_QUERY (available in MySQL): Use buffered queries.

  • PDO::ATTR_DEFAULT_FETCH_MODE: Set default fetch mode. Description of modes is available in PDOStatement::fetch() documentation.

Return Values

Returns TRUE on success or FALSE on failure.

Коментарии

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
2006-07-25 21:44:04
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
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
2007-02-14 22:32:38
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
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
2007-05-18 10:59:44
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
Автор:
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);
?>
2011-01-11 19:49:30
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
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.
2015-12-22 08:51:36
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
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.
2017-07-04 10:21:37
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
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. :)
2017-08-12 02:36:24
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
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;
}
2017-11-09 17:07:57
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
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_PREPAREStrue);
?>
the query was executed successfully.
2017-12-19 16:29:18
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
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());
}
?>
2017-12-22 18:42:18
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
Where would I find the default values of attributes?
2018-11-07 17:10:27
http://php5.kiev.ua/manual/ru/pdo.setattribute.html
Автор:
Note that contrary to most PDO methods, setAttribute does not throw a PDOException when it returns false.
2021-10-19 16:15:32
http://php5.kiev.ua/manual/ru/pdo.setattribute.html

    Поддержать сайт на родительском проекте КГБ