略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: PDO::getAttribute

2024-12-22

PDO::getAttribute

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

PDO::getAttribute 取回一个数据库连接的属性

说明

PDO::getAttribute(int $attribute): mixed

此函数(方法)返回一个数据库连接的属性值。 取回 PDOStatement 属性,请参阅 PDOStatement::getAttribute()

注意有些数据库/驱动可能不支持所有的数据库连接属性。

参数

attribute

PDO::ATTR_* 常量中的一个。下列为应用到数据库连接中的常量:

  • 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

范例

示例 #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 noteadd a note

User Contributed Notes 6 notes

up
7
Phil Hilton
4 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
4
Robert Parham
6 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
grzegorz dot adam dot kowalski at gmail dot com
2 years ago
On PHP 7.4.4 an unsuccessful call returns FALSE, not NULL.
up
0
756567406 at qq dot com
5 years ago
Mysql on version  "5.6.29" not support "PDO::ATTR_PREFETCH"  and "PDO::ATTR_TIMEOUT"
up
-3
mfinkyr at gmail dot com
6 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
-29
peter dot hopfgartner at r3-gis dot com
12 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 ...

官方地址:https://www.php.net/manual/en/pdo.getattribute.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3