Reads a connection-level PDO attribute such as the active driver name.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Reads a connection-level PDO attribute such as the active driver name.
Syntax and Return Value
PDO::getAttribute(Parameter #0 [ <required> int $attribute ]): mixed
The return value and exceptional cases depend on the call described here: Some attributes are driver-dependent; unsupported requests can raise an exception.
Example 1: Basic Use
This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
echo $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
Example 2: A Second Scenario
This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver. This additional check distinguishes the method's result from connection or statement state.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
echo $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
echo $pdo->getAttribute(PDO::ATTR_ERRMODE);
Example 3: Practical Checks
This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver. This additional check distinguishes the method's result from connection or statement state.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
echo $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
echo $pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE);
Important Behavior and Edge Cases
Some attributes are driver-dependent; unsupported requests can raise an exception.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| PDO::getAttribute | Reads a connection-level PDO attribute such as the active driver name. |
| Related API / pattern | setAttribute changes a connection attribute rather than reading it. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | Some attributes are driver-dependent; unsupported requests can raise an exception. |
| Using a related API interchangeably | setAttribute changes a connection attribute rather than reading it. |
Key Takeaways
- Reads a connection-level PDO attribute such as the active driver name.
- Some attributes are driver-dependent; unsupported requests can raise an exception.
- setAttribute changes a connection attribute rather than reading it.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.