ZetCode

PHP PDOStatement::setAttribute Method

last modified April 19, 2025

The PDOStatement::setAttribute method allows configuration of statement-level attributes for PDO prepared statements. It customizes how statements behave.

Basic Definition

PDOStatement::setAttribute sets an attribute on the statement handle. It affects how the statement behaves during execution and fetching results.

Syntax: public PDOStatement::setAttribute(int $attribute, mixed $value): bool. Returns true on success or false on failure. Throws PDOException for invalid attributes.

Setting Fetch Mode for All Fetches

This example shows how to set the default fetch mode for all fetch operations.

set_fetch_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);
    
    $stmt = $pdo->prepare('SELECT * FROM users');
    $stmt->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $stmt->execute();
    
    while ($row = $stmt->fetch()) {
        print_r($row);
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This sets the default fetch mode to associative array for all fetch calls. The PDO::ATTR_DEFAULT_FETCH_MODE attribute affects subsequent fetch operations.

Setting Cursor Type

This demonstrates configuring the cursor type for statement execution.

set_cursor_type.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);
    
    $stmt = $pdo->prepare('SELECT * FROM large_table');
    $stmt->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL);
    $stmt->execute();
    
    // Scrollable cursor allows fetching rows in any order
    $row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_LAST);
    print_r($row);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This sets a scrollable cursor allowing random access to result rows. PDO::CURSOR_SCROLL enables fetching rows in any order after execution.

Setting Timeout for Queries

This shows how to set a timeout for statement execution.

set_timeout.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);
    
    $stmt = $pdo->prepare('SELECT * FROM large_table WHERE complex_condition = ?');
    $stmt->setAttribute(PDO::ATTR_TIMEOUT, 5); // 5 second timeout
    $stmt->execute([1]);
    
    $results = $stmt->fetchAll();
    print_r($results);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This sets a 5-second timeout for query execution. The PDO::ATTR_TIMEOUT attribute specifies the timeout in seconds. Not all drivers support this.

Setting Emulated Prepares

This example shows how to control prepared statement emulation.

set_emulated_prepares.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);
    
    $stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
    $stmt->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $stmt->execute([42]);
    
    $user = $stmt->fetch();
    print_r($user);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This disables emulated prepares, forcing true prepared statements. PDO::ATTR_EMULATE_PREPARES=false uses native database prepares when possible.

Setting Stringify Fetch

This demonstrates converting numeric values to strings during fetch.

set_stringify_fetch.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);
    
    $stmt = $pdo->prepare('SELECT id, name, balance FROM accounts');
    $stmt->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
    $stmt->execute();
    
    $account = $stmt->fetch();
    var_dump($account['balance']); // Will be string even if numeric in DB
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This forces numeric values to be returned as strings. PDO::ATTR_STRINGIFY_FETCHES converts all fetched values to strings. Useful for strict type checking.

Setting Column Case

This shows how to control the case of column names in result sets.

set_column_case.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);
    
    $stmt = $pdo->prepare('SELECT user_id, user_name FROM users');
    $stmt->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
    $stmt->execute();
    
    $user = $stmt->fetch();
    // Column names will be lowercase regardless of database case
    echo $user['user_id']; 
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This forces column names to lowercase in the result set. PDO::ATTR_CASE with PDO::CASE_LOWER converts all column names to lowercase. Other options are CASE_UPPER and CASE_NATURAL.

Setting Persistent Statements

This example demonstrates using persistent statements for performance.

set_persistent.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password', [
        PDO::ATTR_PERSISTENT => true
    ]);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare('SELECT * FROM frequently_accessed');
    $stmt->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['MyPDOStatement']);
    $stmt->execute();
    
    $results = $stmt->fetchAll();
    print_r($results);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

class MyPDOStatement extends PDOStatement {
    // Custom statement class for persistent connections
}

This uses a custom statement class with persistent connections. PDO::ATTR_STATEMENT_CLASS specifies a custom statement class. Works with persistent connections to maintain state between requests.

Best Practices

Source

PHP PDOStatement::setAttribute Documentation

This tutorial covered the PDOStatement::setAttribute method with practical examples showing different configuration options for prepared statements.

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.