PHP PDO tutorial shows how to work with databases using PDO in PHP. Learn PDO with practical examples.
last modified April 19, 2025
The PDO::setAttribute method in PHP configures database connection behavior. It allows setting various attributes that control how PDO operates.
PDO::setAttribute sets an attribute on the database connection handle. Attributes control aspects like error handling, case sensitivity, and more.
Syntax: PDO::setAttribute(int $attribute, mixed $value): bool. The method takes an attribute constant and its value, returning true on success.
This example shows how to configure PDO to throw exceptions on errors.
set_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);
// This will throw an exception if table doesn't exist
$pdo->query('SELECT * FROM non_existent_table');
} catch (PDOException $e) { echo “Error: " . $e->getMessage(); }
Setting PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION makes PDO throw exceptions. This is the recommended error handling mode for development environments.
This demonstrates how to enable persistent database connections.
persistent_connection.php
<?php
declare(strict_types=1);
try { $pdo = new PDO(‘mysql:host=localhost;dbname=testdb’, ‘user’, ‘password’, [ PDO::ATTR_PERSISTENT => true ]);
// Or set it after connection
$pdo->setAttribute(PDO::ATTR_PERSISTENT, true);
echo "Using persistent connection";
} catch (PDOException $e) { echo “Connection failed: " . $e->getMessage(); }
Persistent connections remain open after script execution, reducing overhead. They can be set in constructor options or via setAttribute. Use with caution.
This shows how to set the default fetch mode for all queries.
default_fetch_mode.php
<?php
declare(strict_types=1);
try { $pdo = new PDO(‘mysql:host=localhost;dbname=testdb’, ‘user’, ‘password’); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$stmt = $pdo->query('SELECT * FROM users LIMIT 1');
$user = $stmt->fetch(); // Will use FETCH_ASSOC
print_r($user);
} catch (PDOException $e) { echo “Error: " . $e->getMessage(); }
PDO::ATTR_DEFAULT_FETCH_MODE sets how rows are fetched by default. PDO::FETCH_ASSOC returns results as associative arrays without numeric indices.
This example demonstrates controlling column name case sensitivity.
case_sensitivity.php
<?php
declare(strict_types=1);
try { $pdo = new PDO(‘mysql:host=localhost;dbname=testdb’, ‘user’, ‘password’); $pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$stmt = $pdo->query('SELECT firstName FROM users LIMIT 1');
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Column name will be lowercase
echo $user['firstname'];
} catch (PDOException $e) { echo “Error: " . $e->getMessage(); }
PDO::ATTR_CASE forces column names to a specific case. PDO::CASE_LOWER makes all column names lowercase. PDO::CASE_UPPER makes them uppercase.
This shows how to configure the connection timeout for PDO.
connection_timeout.php
<?php
declare(strict_types=1);
try { $pdo = new PDO(‘mysql:host=localhost;dbname=testdb’, ‘user’, ‘password’); $pdo->setAttribute(PDO::ATTR_TIMEOUT, 5);
echo "Connection timeout set to 5 seconds";
} catch (PDOException $e) { echo “Error: " . $e->getMessage(); }
PDO::ATTR_TIMEOUT sets the timeout in seconds for connection attempts. Not all drivers support this attribute. Check your database documentation.
This demonstrates enabling emulated prepared statements in PDO.
emulated_prepared.php
<?php
declare(strict_types=1);
try { $pdo = new PDO(‘mysql:host=localhost;dbname=testdb’, ‘user’, ‘password’); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([1]);
$user = $stmt->fetch();
print_r($user);
} catch (PDOException $e) { echo “Error: " . $e->getMessage(); }
PDO::ATTR_EMULATE_PREPARES makes PDO emulate prepared statements in PHP. This can improve performance but may be less secure with some databases.
This example shows how to control string conversion behavior.
string_conversion.php
<?php
declare(strict_types=1);
try { $pdo = new PDO(‘mysql:host=localhost;dbname=testdb’, ‘user’, ‘password’); $pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
$stmt = $pdo->query('SELECT id, name FROM users LIMIT 1');
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// All values will be strings
var_dump($user);
} catch (PDOException $e) { echo “Error: " . $e->getMessage(); }
PDO::ATTR_STRINGIFY_FETCHES converts all fetched data to strings. This is useful when working with numeric data that needs string handling.
Error Handling: Always set ERRMODE_EXCEPTION for safety.
Persistent Connections: Use carefully in high-traffic apps.
Fetch Modes: Set default mode matching your needs.
Case Sensitivity: Be consistent with column name casing.
Timeouts: Set appropriate values for your environment.
PHP PDO::setAttribute Documentation
This tutorial covered the PDO::setAttribute method with practical examples. Understanding these settings helps optimize database interactions in PHP.
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.