ZetCode

PHP PDOStatement::getAttribute Method

last modified April 19, 2025

The PDOStatement::getAttribute method retrieves a statement attribute value. It provides access to metadata about the prepared statement's configuration.

Basic Definition

PDOStatement::getAttribute fetches the value of a statement-level attribute. These attributes control how the statement behaves during execution.

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

Getting the Default Fetch Mode

This example shows how to retrieve the current fetch mode of a statement.

get_fetch_mode.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $stmt = $pdo->prepare('SELECT * FROM users');
    
    $fetchMode = $stmt->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE);
    echo "Default fetch mode: " . $fetchMode;
    
    // Common fetch mode values:
    // PDO::FETCH_ASSOC = 2
    // PDO::FETCH_BOTH = 4
    // PDO::FETCH_OBJ = 5
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This retrieves the default fetch mode set on the statement. The value will match one of PDO's FETCH_* constants. The example shows common mode values.

Checking Statement Error Mode

This demonstrates how to check the error handling mode of a statement.

get_error_mode.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $stmt = $pdo->prepare('SELECT * FROM users');
    
    $errorMode = $stmt->getAttribute(PDO::ATTR_ERRMODE);
    
    if ($errorMode === PDO::ERRMODE_EXCEPTION) {
        echo "Statement is set to throw exceptions on errors";
    } elseif ($errorMode === PDO::ERRMODE_WARNING) {
        echo "Statement will generate warnings on errors";
    } else {
        echo "Statement will silently ignore errors";
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This checks how the statement handles errors. The value will be one of PDO::ERRMODE_SILENT, PDO::ERRMODE_WARNING, or PDO::ERRMODE_EXCEPTION.

Getting Cursor Type

This example retrieves the cursor type configured for the statement.

get_cursor_type.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $stmt = $pdo->prepare('SELECT * FROM users');
    
    $cursorType = $stmt->getAttribute(PDO::ATTR_CURSOR);
    
    if ($cursorType === PDO::CURSOR_FWDONLY) {
        echo "Forward-only cursor (default)";
    } elseif ($cursorType === PDO::CURSOR_SCROLL) {
        echo "Scrollable cursor";
    } else {
        echo "Unknown cursor type: " . $cursorType;
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This checks whether the statement uses a forward-only or scrollable cursor. Most drivers default to PDO::CURSOR_FWDONLY for better performance.

Checking Statement Stringify

This shows how to check if the statement converts numeric values to strings.

get_stringify.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $stmt = $pdo->prepare('SELECT * FROM products WHERE price > :price');
    
    $stringify = $stmt->getAttribute(PDO::ATTR_STRINGIFY_FETCHES);
    
    echo "Stringify fetches: " . ($stringify ? 'ON' : 'OFF');
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This checks if numeric values are converted to strings when fetched. PDO::ATTR_STRINGIFY_FETCHES controls this behavior. It's often false by default.

Getting Statement Timeout

This example retrieves the timeout setting for the statement execution.

get_timeout.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $stmt = $pdo->prepare('SELECT * FROM large_table');
    
    $timeout = $stmt->getAttribute(PDO::ATTR_TIMEOUT);
    
    if ($timeout === false) {
        echo "Timeout not supported by driver";
    } else {
        echo "Statement timeout: " . $timeout . " seconds";
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This gets the statement execution timeout in seconds. Some drivers may not support timeouts, in which case the method returns false.

Checking Emulated Prepares

This checks whether the statement uses emulated prepared statements.

get_emulate_prepares.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
    
    $emulated = $stmt->getAttribute(PDO::ATTR_EMULATE_PREPARES);
    
    echo "Prepared statements are " . ($emulated ? "emulated" : "native");
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This determines if the driver emulates prepared statements or uses native prepared statements. Emulation may be used for drivers that lack native support.

Getting Driver-Specific Attributes

This demonstrates retrieving driver-specific statement attributes.

get_driver_attributes.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $stmt = $pdo->prepare('SELECT * FROM users');
    
    // MySQL-specific attribute
    if ($pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql') {
        $bufferSize = $stmt->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
        echo "Buffered query: " . ($bufferSize ? 'ON' : 'OFF');
    }
    
    // SQLite-specific attribute
    if ($pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'sqlite') {
        $busyTimeout = $stmt->getAttribute(PDO::SQLITE_ATTR_READONLY_STATEMENT);
        echo "SQLite busy timeout: " . $busyTimeout;
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This shows how to get driver-specific attributes. First check the driver name, then request driver-specific attributes. Each driver has its own constants.

Best Practices

Source

PHP PDOStatement::getAttribute Documentation

This tutorial covered the PDOStatement::getAttribute method with practical examples showing how to retrieve various statement 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.