PHP Australia Conference 2015

PDO::setAttribute

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

PDO::setAttribute Establece un atributo

Descripción

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

Establece un atributo en el manejador de la base de datos. Algunos de los atributos genéricos disponibles están listados a continuación; algunos drivers pueden hacer uso de atributos adicionales específicos.

  • PDO::ATTR_CASE: Fuerza a los nombres de columnas a una capitalización específica.

    • PDO::CASE_LOWER: Fuerza a los nombres de columnas a minúsculas.

    • PDO::CASE_NATURAL: Deja los nombres de columnas como son devueltas por el driver de la base de datos.

    • PDO::CASE_UPPER: Fuerza a los nombres de columnas a mayúsculas.

  • PDO::ATTR_ERRMODE: Reporte de errores.

    • PDO::ERRMODE_SILENT: Establece los códigos de error.

    • PDO::ERRMODE_WARNING: Eleva E_WARNING.

    • PDO::ERRMODE_EXCEPTION: Lanza exceptions.

  • PDO::ATTR_ORACLE_NULLS (disponible para todos los drivers, no sólo Oracle): Conversión de NULL y cadenas vacías.

    • PDO::NULL_NATURAL: Sin conversión.

    • PDO::NULL_EMPTY_STRING: Las cadenas vacías son convertidas a NULL.

    • PDO::NULL_TO_STRING: NULL se convierte a cadenas vacías.

  • PDO::ATTR_STRINGIFY_FETCHES: Convierte los valores numéricos a cadenas cuando se buscan. Requiere bool.

  • PDO::ATTR_STATEMENT_CLASS: Establece la clase de sentencia proporcionada por el usuario derivada de PDOStatement. No puede ser usado con instancias PDO persistentes. Requiere array(string classname, array(mixed constructor_args)).

  • PDO::ATTR_TIMEOUT: Especifica la duración del tiempo de espera en segundos 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.

Valores devueltos

Devuelve TRUE en caso de éxito o FALSE en caso de error.

add a note add a note

User Contributed Notes 4 notes

up
5
gregory dot szorc at gmail dot com
7 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
6
antoyo
3 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
3
colinganderson [at] gmail [dot] com
7 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
2
m dot leuffen at gmx dot de
8 years ago
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
To Top