Repository navigation
PDO::getAttribute(): document which attributes every driver supports - #6076
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
PDO::getAttribute() handles eight generic attributes itself (ATTR_CASE, ATTR_DEFAULT_FETCH_MODE, ATTR_DRIVER_NAME, ATTR_ERRMODE, ATTR_ORACLE_NULLS, ATTR_PERSISTENT, ATTR_STATEMENT_CLASS and ATTR_STRINGIFY_FETCHES), so they can be retrieved with every driver. Everything else, including generic attributes such as ATTR_AUTOCOMMIT, ATTR_SERVER_VERSION or ATTR_TIMEOUT, is passed to the driver, and each driver only supports some of them. The page listed the eight as "the generic attributes that apply to database connections" and only said that some driver combinations "may not support all" attributes, which did not say which attributes are safe to use everywhere (php#4436). - State that the listed attributes are handled by PDO and work with every driver, and that support for any other attribute depends on the driver. - Fix the return value: a failure returns false, not null. Note that some attributes can themselves be false, so a failure is best detected through the exception error mode or PDO::errorCode(). - Errors: an IM001 error is raised when the driver does not support the attribute; use the standard &pdo.errors; text for how it is reported. - Replace the example. It requested ATTR_PREFETCH, ATTR_TIMEOUT and ATTR_CONNECTION_STATUS from PDO_ODBC, which supports none of them, so it threw on the first of these. The new one uses SQLite, catches the PDOException for unsupported attributes and shows its output. Checked against ext/pdo/pdo_dbh.c and the get_attribute handlers of the bundled drivers, and by running the example on PHP 8.4. Fixes php#4436
kamil-tekiela
requested changes
Nov 13, 2026
Comment on lines
+81
to
+83
| a failure is best detected with the | ||
| <constant>PDO::ERRMODE_EXCEPTION</constant> error mode, or by checking | ||
| <methodname>PDO::errorCode</methodname>. |
Member
There was a problem hiding this comment.
Well, that's not entirely true. It can return false without setting an error message.
Comment on lines
+90
to
+91
| An error with SQLSTATE <literal>IM001</literal> is raised if the driver | ||
| does not support retrieving <parameter>attribute</parameter>. |
Member
There was a problem hiding this comment.
I wouldn't include it here, though. This is pretty much an implementation detail and it's not guaranteed. By stating this information in the manual we are enforcing on implementations that this has to be true.
Comment on lines
+54
to
+55
| All other attributes are handled by the driver, and which of them | ||
| can be retrieved varies from driver to driver. |
Member
There was a problem hiding this comment.
This is a run-on sentence and would be better split up.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PDO::getAttribute()handles eight generic attributes itself (ATTR_CASE,ATTR_DEFAULT_FETCH_MODE,ATTR_DRIVER_NAME,ATTR_ERRMODE,ATTR_ORACLE_NULLS,ATTR_PERSISTENT,ATTR_STATEMENT_CLASSandATTR_STRINGIFY_FETCHES), so every driver supports them. It passes every other attribute to the driver. That includes generic ones such asATTR_AUTOCOMMIT,ATTR_SERVER_VERSIONandATTR_TIMEOUT, and each driver supports only some of them. The page didn't make this distinction, which is what #4436 is about.This updates the page:
attributeparameter now say that the listed attributes work with every driver and that support for any other attribute depends on the driver.false, notnull. A new note explains that some attributes can themselves befalse, so the exception error mode orPDO::errorCode()is the reliable way to detect a failure.IM001error is raised when the driver does not support the attribute, followed by the standard&pdo.errors;text.ATTR_PREFETCH,ATTR_TIMEOUTandATTR_CONNECTION_STATUS, which that driver doesn't support, so it threw. The new example uses SQLite, includesATTR_DRIVER_NAME, catches thePDOExceptionfor unsupported attributes and shows its output. This covers the points in the follow-up comment on the issue.I checked this against
ext/pdo/pdo_dbh.cand theget_attributehandlers of the bundled drivers. I also ran the example on PHP 8.4, andconfigure.phpand docbook-cs pass.Fixes #4436