PDO::getAttribute

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.2.0)

PDO::getAttribute データベース接続の属性を取得する

説明

public PDO::getAttribute(int $attribute): mixed

この関数はデータベース接続の属性値を返します。PDOStatement 属性を取得する場合、PDOStatement::getAttribute() を参照ください。

いくつかのデータベースもしくはドライバは、 データベース接続の属性の全てのをサポートしていないかも知れないことに 注意してください。

パラメータ

attribute

PDO_ATTR_* 定数の 1 つを指定します。 データベース接続に適用される一般的な属性は以下の通りです。

  • 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

ドライバによっては、 そのドライバ固有の属性を使っているかもしれません。 そういう属性は、他のドライバでは使っては いけない ことに注意して下さい。

戻り値

コールに成功した場合は要求された PDO 属性の値を返します。 コールに失敗した場合は null を返します。

エラー / 例外

ドライバが要求された attribute をサポートしていない場合、 PDO::getAttribute()PDOException をスローします。

例1 データベース接続の属性を取得する

<?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")) . "\n";
}
?>

参考

add a note add a note

User Contributed Notes 6 notes

up
5
Phil Hilton
5 years ago
Better example that handles unsupported attributes gracefully:

<?php

$conn
= new PDO( 'odbc:sample', 'db2inst1', 'ibmdb2' );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$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: ";
    try {
        echo
$conn->getAttribute( constant( "PDO::ATTR_$val" ) ) . "\n";
    } catch (
PDOException $e ) {
        echo
$e->getMessage() . "\n";
    }
}

?>
up
2
Robert Parham
8 years ago
Oracle does not have the following attributes:

PDO::ATTR_CONNECTION_STATUS: SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute
PDO::ATTR_PREFETCH: SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute
PDO::ATTR_TIMEOUT: SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute

The rest work fine.
up
1
756567406 at qq dot com
7 years ago
Mysql on version  "5.6.29" not support "PDO::ATTR_PREFETCH"  and "PDO::ATTR_TIMEOUT"
up
-1
mfinkyr at gmail dot com
8 years ago
As of 30-Jan-2016, MariaDB on version "5.5.5-10.1.9-MariaDB" apparently  does not support: "PREFETCH" nor "TIMEOUT".
up
-1
grzegorz dot adam dot kowalski at gmail dot com
4 years ago
On PHP 7.4.4 an unsuccessful call returns FALSE, not NULL.
up
-29
peter dot hopfgartner at r3-gis dot com
14 years ago
The Oracle driver seems to not support PDO::getAttribute():

ociPHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[IM001]: Driver does not support this function: driver does not support getting attributes' in ...
To Top