It should be noted that PDO::FETCH_CLASS will call the constructor *after* setting the values (or calling __set).
PHP - Manual: 预定义常量
2024-12-22
下列常量由此扩展定义,且仅在此扩展编译入 PHP 或在运行时动态载入时可用。
自 PHP 5.1 起,开始使用类常量。以前的版本使用类似 PDO_PARAM_BOOL
这样的全局常量。
PDO::PARAM_BOOL
(integer)
PDO::PARAM_NULL
(integer)
PDO::PARAM_INT
(integer)
PDO::PARAM_STR
(integer)
PDO::PARAM_STR_NATL
(integer)
PDO::PARAM_STR_CHAR
(integer)
PDO::PARAM_LOB
(integer)
PDO::PARAM_STMT
(integer)
PDO::PARAM_INPUT_OUTPUT
(integer)
PDO::FETCH_LAZY
(integer)
PDO::FETCH_LAZY
创建用来访问的对象变量名。在 PDOStatement::fetchAll() 中无效。
PDO::FETCH_ASSOC
(integer)
PDO::FETCH_ASSOC
每个列名只返回一个值。
PDO::FETCH_NAMED
(integer)
PDO::FETCH_ASSOC
每个列名 返回一个包含值的数组。
PDO::FETCH_NUM
(integer)
PDO::FETCH_BOTH
(integer)
PDO::FETCH_OBJ
(integer)
PDO::FETCH_BOUND
(integer)
PDO::FETCH_COLUMN
(integer)
PDO::FETCH_CLASS
(integer)
注意: 如果所请求的类中不存在该属性,则调用 __set() 魔术方法
PDO::FETCH_INTO
(integer)
PDO::FETCH_FUNC
(integer)
PDO::FETCH_GROUP
(integer)
PDO::FETCH_COLUMN
或 PDO::FETCH_KEY_PAIR
一起使用。
PDO::FETCH_UNIQUE
(integer)
PDO::FETCH_KEY_PAIR
(integer)
PDO::FETCH_CLASSTYPE
(integer)
PDO::FETCH_SERIALIZE
(integer)
PDO::FETCH_INTO
,但是以一个序列化的字符串表示对象。自 PHP 5.1.0 起可用。从 PHP 5.3.0 开始,如果设置此标志,则类的构造函数从不会被调用。
PDO::FETCH_PROPS_LATE
(integer)
PDO::ATTR_AUTOCOMMIT
(integer)
false
,PDO 将试图禁用自动提交以便数据库连接开始一个事务。
PDO::ATTR_PREFETCH
(integer)
PDO::ATTR_TIMEOUT
(integer)
PDO::ATTR_ERRMODE
(integer)
PDO::ATTR_SERVER_VERSION
(integer)
PDO::ATTR_CLIENT_VERSION
(integer)
PDO::ATTR_SERVER_INFO
(integer)
PDO::ATTR_CONNECTION_STATUS
(integer)
PDO::ATTR_CASE
(integer)
PDO::CASE_*
的常量强制列名为指定的大小写。
PDO::ATTR_CURSOR_NAME
(integer)
PDO::ATTR_CURSOR
(integer)
PDO::CURSOR_FWDONLY
和 PDO::CURSOR_SCROLL
。一般为 PDO::CURSOR_FWDONLY
,除非确实需要一个可滚动游标。
PDO::ATTR_DRIVER_NAME
(string)
示例 #1 使用 PDO::ATTR_DRIVER_NAME
的例子
<?php
if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
echo "Running on mysql; doing something mysql specific here\n";
}
?>
PDO::ATTR_ORACLE_NULLS
(integer)
PDO::ATTR_PERSISTENT
(integer)
PDO::ATTR_STATEMENT_CLASS
(integer)
PDO::ATTR_FETCH_CATALOG_NAMES
(integer)
PDO::ATTR_FETCH_TABLE_NAMES
(integer)
PDO::ATTR_STRINGIFY_FETCHES
(integer)
PDO::ATTR_MAX_COLUMN_LEN
(integer)
PDO::ATTR_DEFAULT_FETCH_MODE
(integer)
PDO::ATTR_EMULATE_PREPARES
(integer)
PDO::ATTR_DEFAULT_STR_PARAM
(integer)
PDO::PARAM_STR_NATL
和 PDO::PARAM_STR_CHAR
。
自 PHP 7.2.0 起可用
PDO::ERRMODE_SILENT
(integer)
PDO::ERRMODE_WARNING
(integer)
E_WARNING
消息。关于此属性的更多信息请参见 错误与错误处理。
PDO::ERRMODE_EXCEPTION
(integer)
PDO::CASE_NATURAL
(integer)
PDO::CASE_LOWER
(integer)
PDO::CASE_UPPER
(integer)
PDO::NULL_NATURAL
(integer)
PDO::NULL_EMPTY_STRING
(integer)
PDO::NULL_TO_STRING
(integer)
PDO::FETCH_ORI_NEXT
(integer)
PDO::FETCH_ORI_PRIOR
(integer)
PDO::FETCH_ORI_FIRST
(integer)
PDO::FETCH_ORI_LAST
(integer)
PDO::FETCH_ORI_ABS
(integer)
PDO::FETCH_ORI_REL
(integer)
PDO::CURSOR_FWDONLY
(integer)
PDO::CURSOR_SCROLL
(integer)
PDO::FETCH_ORI_*
常量来控制结果集中获取的行。
PDO::ERR_NONE
(string)
PDO::PARAM_EVT_ALLOC
(integer)
PDO::PARAM_EVT_FREE
(integer)
PDO::PARAM_EVT_EXEC_PRE
(integer)
PDO::PARAM_EVT_EXEC_POST
(integer)
PDO::PARAM_EVT_FETCH_PRE
(integer)
PDO::PARAM_EVT_FETCH_POST
(integer)
PDO::PARAM_EVT_NORMALIZE
(integer)
PDO::SQLITE_DETERMINISTIC
(integer)
It should be noted that PDO::FETCH_CLASS will call the constructor *after* setting the values (or calling __set).
PDO::FETCH_UNIQUE not only fetches the unique values, it also uses the first SQL column as array key result, what is very useful for create quickly an index, eg :
<?php
$sql = <<<SQL
SELECT ALL
c1, -- For result indexing
c1, c2
FROM (
VALUES
ROW('ID-1', 'Value 1'),
ROW('ID-2', 'Value 2a'),
ROW('ID-2', 'Value 2b'),
ROW('ID-3', 'Value 3')
) AS t (c1, c2);
SQL;
$result = $pdo->query($sql);
print_r($result->fetchAll(PDO::FETCH_UNIQUE));
/*
Gives :
ID-1 => [c1 => ID-1, c2 => Value 1]
ID-2 => [c1 => ID-2b, c2 => Value 2b]
ID-3 => [c1 => ID-3, c2 => Value 3]
*/
?>
Reference to all the PDO::MYSQL_* constants is available in MySQL's documentation for the driver here: https://dev.mysql.com/doc/connectors/en/apis-php-pdo-mysql.html
Such as the ones I was looking for PDO::MYSQL_ATTR_SSL_CA and PDO::MYSQL_ATTR_SSL_CAPATH which are not listed on this page.
PDO::PARAM_STR_CHAR and PDO::PARAM_STR_NATL must be combined with PDO::PARAM_STR using bitwise-OR for parameter binding.
These flags control value quoting (e.g. PDO::quote) and, in some situations (see below), parameter binding (e.g. PDO::bindParam, PDO::bindValue) to prefix string literals with N'' as defined in SQL-92. As of PHP 7.3, only dblib and mysql support these flags. For the mysql driver, the flags only affect parameter binding when PDO::ATTR_EMULATE_PREPARES is true (the default).
MySQL and MariaDB interpret string literals prefixed with N as being utf8 (not utf8mb4) regardless of `SET NAMES` or the charset parameter. This can cause problems if the database/table/column charset is not utf8. For example, in a database using utf8mb4, the query "SELECT * FROM table WHERE col = :param" and bindValue(":param", "\u{1F600}", PDO::PARAM_STR | PDO::PARAM_STR_NATL) will cause "PDOException: SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='". Using PDO::PARAM_STR without PDO::PARAM_STR_NATL and ensuring the charset DSN parameter is set correctly can avoid this issue.
See:
https://wiki.php.net/rfc/extended-string-types-for-pdo
https://mariadb.com/kb/en/library/string-literals/
https://dev.mysql.com/doc/refman/8.0/en/string-literals.html
Default value for \PDO::ATTR_TIMEOUT is 30 seconds.
Ref.: https://github.com/php/php-src/blob/PHP-7.1.0/ext/pdo_mysql/mysql_driver.c#L600
When you use REAL type column on SQLite, your setting of pdo parameter of bindValue() is PDO::PARAM_STR.
'''
$stmt->bindValue(':elapse_time', $e_time, PDO::PARAM_STR);
'''