PHP 8.3.4 Released!

PDO::getAttribute

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

PDO::getAttribute Recuperar um atributo da conexão com o banco de dados

Descrição

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

Esta função retorna o valor de um atributo da conexão com o banco de dados. Para recuperar atributos de PDOStatement consulte PDOStatement::getAttribute().

Note que algumas combinações de banco de dados e drivers podem não suportar todos os atributos da conexão com o banco de dados.

Parâmetros

attribute

Uma das constantes PDO::ATTR_*. Os atributos genéricos que se aplicam a conexões com o banco de dados são os seguintes:

  • 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

Alguns drivers podem fazer uso de atributos específicos adicionais. Note que atributos específicos de drivers não devem ser usados com outros drivers.

Valor Retornado

Uma chamada com sucesso retornará o valor do atributo PDO solicitado. Uma chamada sem sucesso retornará null.

Erros/Exceções

PDO::getAttribute() pode lançar uma PDOException quando o driver subjacente não suportar o attribute requerido.

Exemplos

Exemplo #1 Recuperando atributos de uma conexão com o banco de dados

<?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";
}
?>

Veja Também

add a note

User Contributed Notes 2 notes

up
6
Phil Hilton
5 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
8 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