PDO::setAttribute

PHP PDO 参考手册PHP PDO Reference Manual

PDO::setAttribute — Sets an attribute (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)


Description

Syntax

bool PDO::setAttribute ( int $attribute , mixed $value )

Sets attributes on the database handle. Listed below are some available generic attributes; some drivers may use additional driver-specific attributes.

  • PDO::ATTR_CASE: Force column names to the specified case.

    • PDO::CASE_LOWER: Force column names to lower case.

    • PDO::CASE_NATURAL: Leave column names as returned by the database driver.

    • PDO::CASE_UPPER: Force column names to upper case.

  • PDO::ATTR_ERRMODE: Error reporting.

    • PDO::ERRMODE_SILENT: Set error codes only.

    • PDO::ERRMODE_WARNING: RaiseE_WARNINGerror

    • PDO::ERRMODE_EXCEPTION: Throwexceptionsan exception.

  • PDO::ATTR_ORACLE_NULLS(Available in all drivers, not just Oracle): Convert NULL and empty strings.

    • PDO::NULL_NATURAL: No conversion.

    • PDO::NULL_EMPTY_STRING: Convert empty string toNULL。

    • PDO::NULL_TO_STRING: Convert NULL to an empty string.

  • PDO::ATTR_STRINGIFY_FETCHES: Convert numeric values to strings when fetching. Requiresbool。

  • PDO::ATTR_STATEMENT_CLASS: Set the user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requiresarray(string class name, array(mixed constructor parameters))。

  • PDO::ATTR_TIMEOUT: Specifies the number of seconds for the timeout. Not all drivers support this option, meaning there may be differences between drivers. For example, SQLite gives up obtaining a write lock once the wait time reaches this value, but other drivers may interpret this value as a connection or read timeout interval. Requires an int type.

  • PDO::ATTR_AUTOCOMMIT(Available in OCI, Firebird, and MySQL): Whether to automatically commit each individual statement.

  • PDO::ATTR_EMULATE_PREPARESEnable or disable emulation of prepared statements. Some drivers do not support native prepared statements or have limited support. Use this setting to force PDO to always emulate prepared statements (if set toTRUE), or try to use native prepared statements (if set toFALSE). If the driver cannot successfully prepare the current query, it will always fall back to emulating prepared statements. Requiresbooltype.

  • PDO::MYSQL_ATTR_USE_BUFFERED_QUERY(Available in MySQL): Use buffered queries.

  • PDO::ATTR_DEFAULT_FETCH_MODE: Set the default fetch mode. For a description of the modes, see thePDOStatement::fetch()documentation.


Return Values

Returns TRUE on success, or FALSE on failure.

Other Extensions