PDO::getAttribute

Retrieve a database connection attribute

Description

public mixed PDO::getAttribute(int $attribute)

This function returns the value of a database connection attribute. To retrieve PDOStatement attributes, refer to PDOStatement::getAttribute.

Only the generic attributes listed below can be retrieved with every driver. Whether any other attribute can be retrieved depends on the driver.

Parameters

attribute

One of the PDO::ATTR_* constants. The following generic attributes are handled by PDO itself and can be retrieved with every driver:

  • PDO::ATTR_CASE
  • PDO::ATTR_DEFAULT_FETCH_MODE
  • PDO::ATTR_DRIVER_NAME
  • PDO::ATTR_ERRMODE
  • PDO::ATTR_ORACLE_NULLS
  • PDO::ATTR_PERSISTENT
  • PDO::ATTR_STATEMENT_CLASS
  • PDO::ATTR_STRINGIFY_FETCHES

All other attributes are handled by the driver. Which of them can be retrieved varies from driver to driver. This includes generic attributes such as PDO::ATTR_AUTOCOMMIT, PDO::ATTR_CLIENT_VERSION, PDO::ATTR_CONNECTION_STATUS, PDO::ATTR_SERVER_INFO, PDO::ATTR_SERVER_VERSION and PDO::ATTR_TIMEOUT, as well as driver specific attributes. Note that driver specific attributes must not be used with other drivers.

Return Values

Returns the value of the requested attribute, or false on failure.

The value of some attributes, such as PDO::ATTR_PERSISTENT, can itself be false, so a false return value does not always indicate a failure.

Errors/Exceptions

PDO::getAttribute may throw a PDOException when the underlying driver does not support the requested attribute.

Changelog

Version Description
8.4.0 It is now possible to fetch the value of the PDO::ATTR_STRINGIFY_FETCHES attribute.
8.4.0 The value of PDO::ATTR_AUTOCOMMIT is now returned as bool by the MySQL and Firebird drivers, and the value of PDO::ATTR_EMULATE_PREPARES and PDO::MYSQL_ATTR_DIRECT_QUERY is now returned as bool by the MySQL driver; previously, they were returned as int.

Examples

Example #1 Retrieving database connection attributes

<?php
$pdo = new PDO('sqlite::memory:');

$attributes = [
    'DRIVER_NAME', 'ERRMODE', 'CASE', 'ORACLE_NULLS', 'PERSISTENT',
    'CLIENT_VERSION', 'SERVER_VERSION', 'AUTOCOMMIT', 'TIMEOUT',
];

foreach ($attributes as $name) {
    try {
        $value = $pdo->getAttribute(constant("PDO::ATTR_$name"));
        echo "PDO::ATTR_$name: ", var_export($value, true), "\n";
    } catch (PDOException $e) {
        echo "PDO::ATTR_$name: ", $e->getMessage(), "\n";
    }
}
?>

The above example will output something similar to:

PDO::ATTR_DRIVER_NAME: 'sqlite'
PDO::ATTR_ERRMODE: 2
PDO::ATTR_CASE: 0
PDO::ATTR_ORACLE_NULLS: 0
PDO::ATTR_PERSISTENT: false
PDO::ATTR_CLIENT_VERSION: '3.46.1'
PDO::ATTR_SERVER_VERSION: '3.46.1'
PDO::ATTR_AUTOCOMMIT: 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

See Also

  • PDO::setAttribute
  • PDOStatement::getAttribute
  • PDOStatement::setAttribute