ZetCode

PHP PDO::getAttribute Method

last modified April 19, 2025

The PDO::getAttribute method retrieves database connection attributes in PHP. It provides access to various PDO and driver-specific connection settings.

Basic Definition

PDO::getAttribute fetches the value of a database connection attribute. These attributes can be PDO constants or driver-specific settings.

Syntax: public PDO::getAttribute(int $attribute): mixed. The method takes an attribute constant and returns its current value.

Common attributes include PDO::ATTR_SERVER_INFO, PDO::ATTR_SERVER_VERSION, and PDO::ATTR_DRIVER_NAME. Some attributes are database driver specific.

Getting Database Server Version

This example shows how to retrieve the database server version.

get_server_version.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $version = $pdo->getAttribute(PDO::ATTR_SERVER_VERSION);
    
    echo "Database server version: $version";
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This retrieves the server version using PDO::ATTR_SERVER_VERSION. The value returned depends on the database system being used. For MySQL, it returns the MySQL server version string.

Checking Connection Status

This example demonstrates checking if the connection is persistent.

check_persistent.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password', [
        PDO::ATTR_PERSISTENT => true
    ]);
    
    $isPersistent = $pdo->getAttribute(PDO::ATTR_PERSISTENT);
    
    echo "Connection is " . ($isPersistent ? 'persistent' : 'not persistent');
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This checks if the connection is persistent using PDO::ATTR_PERSISTENT. The method returns true for persistent connections, false otherwise. Persistent connections remain open after script execution ends.

Getting Driver Name

This example shows how to get the name of the database driver.

get_driver_name.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
    
    echo "Database driver: $driver";
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This retrieves the driver name using PDO::ATTR_DRIVER_NAME. For MySQL connections, it returns "mysql". For SQLite, it returns "sqlite". The value helps identify which database system is being used.

Checking Error Mode

This example demonstrates checking the current error handling mode.

check_error_mode.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $errorMode = $pdo->getAttribute(PDO::ATTR_ERRMODE);
    
    echo "Error mode: ";
    switch ($errorMode) {
        case PDO::ERRMODE_SILENT: echo "SILENT"; break;
        case PDO::ERRMODE_WARNING: echo "WARNING"; break;
        case PDO::ERRMODE_EXCEPTION: echo "EXCEPTION"; break;
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This checks the current error mode using PDO::ATTR_ERRMODE. The method returns one of the PDO::ERRMODE_* constants. The example shows how to interpret the returned value.

Getting Client Version

This example retrieves the client library version information.

get_client_version.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $clientVersion = $pdo->getAttribute(PDO::ATTR_CLIENT_VERSION);
    
    echo "Client version: ";
    print_r($clientVersion);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This retrieves client version information using PDO::ATTR_CLIENT_VERSION. For MySQL, it returns an array with client library version details. The exact format depends on the database driver being used.

Checking Case Conversion

This example checks if column names are converted to uppercase or lowercase.

check_case_conversion.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $caseConversion = $pdo->getAttribute(PDO::ATTR_CASE);
    
    echo "Case conversion: ";
    switch ($caseConversion) {
        case PDO::CASE_NATURAL: echo "NATURAL"; break;
        case PDO::CASE_UPPER: echo "UPPER"; break;
        case PDO::CASE_LOWER: echo "LOWER"; break;
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This checks the current case conversion setting using PDO::ATTR_CASE. The method returns one of the PDO::CASE_* constants. This affects how column names are returned in result sets.

Getting Connection Status

This example retrieves server status information.

get_server_info.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $serverInfo = $pdo->getAttribute(PDO::ATTR_SERVER_INFO);
    
    echo "Server info: $serverInfo";
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This retrieves server status information using PDO::ATTR_SERVER_INFO. For MySQL, it returns a string with connection and server status. The exact content depends on the database driver being used.

Best Practices

Source

PHP PDO::getAttribute Documentation

This tutorial covered the PDO::getAttribute method with practical examples. It showed how to retrieve various database connection attributes.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP PDO Functions.