phpday 2027 - Call for Papers

PDO::getAttribute

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

PDO::getAttribute — Retrieve a database connection attribute

Description

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

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:

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

User Contributed Notes 2 notes

up
5
Phil Hilton
8 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
11 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.
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top