PDO::getAttribute – Retrieve a Database Connection Attribute

The article explains the PDO::getAttribute method, lists all PDO::ATTR_* constants that can be queried, describes its return behavior, and provides a PHP example demonstrating how to retrieve multiple connection attributes for debugging.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PDO::getAttribute – Retrieve a Database Connection Attribute

This function (method) returns the value of a PDO database connection attribute.

Parameter

attribute

One of the PDO::ATTR_* constants, such as:

PDO::ATTR_AUTOCOMMIT

PDO::ATTR_CASE

PDO::ATTR_CLIENT_VERSION

PDO::ATTR_CONNECTION_STATUS

PDO::ATTR_DRIVER_NAME

PDO::ATTR_ERRMODE

PDO::ATTR_ORACLE_NULLS

PDO::ATTR_PERSISTENT

PDO::ATTR_PREFETCH

PDO::ATTR_SERVER_INFO

PDO::ATTR_SERVER_VERSION

PDO::ATTR_TIMEOUT

Return value

On success, returns the requested PDO attribute value; on failure, returns null .

Example

<?php
$conn = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');
$attributes = array(
    "AUTOCOMMIT",
    "ERRMODE",
    "CASE",
    "CLIENT_VERSION",
    "CONNECTION_STATUS",
    "ORACLE_NULLS",
    "PERSISTENT",
    "PREFETCH",
    "SERVER_INFO",
    "SERVER_VERSION",
    "TIMEOUT"
);
foreach ($attributes as $val) {
    echo "PDO::ATTR_$val: ";
    echo $conn->getAttribute(constant("PDO::ATTR_$val")) . "
";
}
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackenddatabasePHPexampleAttributesPDO
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.