PHP 8.5.0 Alpha 1 available for testing

PDO::getAttribute

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

PDO::getAttribute Recupera un atributo de una conexión a una base de datos

Descripción

public PDO::getAttribute(int $attribute): mixed

Esta función devuelve el valor de un atributo de una conexión a una base de datos. Para recuperar los atributos PDOStatement, consúltese la función PDOStatement::getAttribute().

Tenga en cuenta que algunas bases de datos/drivers combinados no soportan todos los atributos de conexión.

Parámetros

attribute

Una de las constantes PDO::ATTR_*. Los atributos genéricos que se aplican a las conexiones son los siguientes:

  • PDO::ATTR_AUTOCOMMIT
  • PDO::ATTR_CASE
  • PDO::ATTR_CLIENT_VERSION
  • PDO::ATTR_CONNECTION_STATUS
  • PDO::ATTR_DRIVER_NAME
  • PDO::ATTR_ERRMODE
  • PDO::ATTR_ORACLE_NULLS
  • PDO::ATTR_PERSISTENT
  • PDO::ATTR_PREFETCH
  • PDO::ATTR_SERVER_INFO
  • PDO::ATTR_SERVER_VERSION
  • PDO::ATTR_TIMEOUT

Algunos controladores pueden hacer uso de atributos adicionales específicos del controlador. Tenga en cuenta que los atributos específicos del controlador no deben ser utilizados con otros controladores.

Valores devueltos

Una llamada exitosa devuelve el valor del atributo PDO solicitado. Una llamada que ha fallado devuelve el valor null.

Errores/Excepciones

El método PDO::getAttribute() puede generar una excepción PDOException cuando el controlador subyacente no soporta el attribute solicitado.

Ejemplos

Ejemplo #1 Recuperación de los atributos de conexión a una base de datos

<?php
$conn
= new PDO('odbc:sample', 'db2inst1', 'ibmdb2');
$attributes = array(
"AUTOCOMMIT", "ERRMODE", "CASE", "CLIENT_VERSION", "CONNECTION_STATUS",
"ORACLE_NULLS", "PERSISTENT", "PREFETCH", "SERVER_INFO", "SERVER_VERSION",
"TIMEOUT"
);

foreach (
$attributes as $val) {
echo
"PDO::ATTR_$val: ";
echo
$conn->getAttribute(constant("PDO::ATTR_$val")) . "\n";
}
?>

Ver también

add a note

User Contributed Notes 2 notes

up
5
Phil Hilton
7 years ago
Better example that handles unsupported attributes gracefully:

<?php

$conn
= new PDO( 'odbc:sample', 'db2inst1', 'ibmdb2' );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$attributes = array(
"AUTOCOMMIT", "ERRMODE", "CASE", "CLIENT_VERSION", "CONNECTION_STATUS",
"ORACLE_NULLS", "PERSISTENT", "PREFETCH", "SERVER_INFO", "SERVER_VERSION",
"TIMEOUT"
);

foreach (
$attributes as $val ) {
echo
"PDO::ATTR_$val: ";
try {
echo
$conn->getAttribute( constant( "PDO::ATTR_$val" ) ) . "\n";
} catch (
PDOException $e ) {
echo
$e->getMessage() . "\n";
}
}

?>
up
0
Robert Parham
9 years ago
Oracle does not have the following attributes:

PDO::ATTR_CONNECTION_STATUS: SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute
PDO::ATTR_PREFETCH: SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute
PDO::ATTR_TIMEOUT: SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute

The rest work fine.
To Top