|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44153 PDO->errorCode() returns '' instead of SQLSTATE if no operation has been run
Submitted: 2008-02-18 18:07 UTC Modified: 2008-11-22 20:17 UTC
From: uwendel at mysql dot com Assigned: davidc (profile)
Status: Closed Package: PDO related
PHP Version: 5.3CVS-2008-02-18 (CVS) OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: uwendel at mysql dot com
New email:
PHP Version: OS:


 [2008-02-18 18:07 UTC] uwendel at mysql dot com
The PHP manual states that PDO->errorCode() returns the SQLSTATE associated with the last operation run on the handle, 

"Return Values

Returns a SQLSTATE, a five-character alphanumeric identifier defined in the ANSI SQL-92 standard. Briefly, an SQLSTATE consists of a two-character class value followed by a three-character subclass value. A class value of 01 indicates a warning and is accompanied by a return code of SQL_SUCCESS_WITH_INFO. Class values other than '01', except for the class 'IM', indicate an error. The class 'IM' is specific to warnings and errors that derive from the implementation of PDO (or perhaps ODBC, if you're using the ODBC driver) itself. The subclass value '000' in any class indicates that there is no subclass for that SQLSTATE. "

What is PDO->errorCode() supposed to return if no operation has been run on the handle yet? 

If its valid to return any empty string, please add this to the documentation.

Reproduce code:
nixnutz@ulflinux:~/php53> sapi/cli/php -r '$pdo=new PDO("mysql:dbname=phptest;unix_socket=/tmp/mysql.sock", "root", "root"); var_dump($pdo->errorCode());'
string(0) ""

nixnutz@ulflinux:~/php53> sapi/cli/php -r '$pdo=new PDO("sqlite:/tmp/foo.db"); var_dump($pdo->errorCode());'
string(0) ""

Expected result:
Its a matter of taste. Personally I have a preference for returning '00000' as this makes checking the error code easier but this would be sort of a BC break.

Actual result:
Empty string (see above).


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-24 23:34 UTC]
What about returning NULL ? You could easily check the results using is_null() and if it's null that means there's no errorCode().

Whereas when there's an error code you'll get the usual sqlstate message/code.

What do you think? I think NULL would make great sense.
 [2008-11-01 19:57 UTC]
The patch below allows you to do:

$error = $pdo->errorCode();

if (is_null($error)) {
    // There's an empty error...

More I look at it and more I think we should have a generic message like "UNUSED" or something similar but I think NULL is also quite straightforward.
 [2008-11-04 18:30 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

 [2008-11-22 20:16 UTC]
Documentation has been updated.
 [2008-11-22 20:17 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Jun 07 04:01:28 2020 UTC