php.net |  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
 [2008-02-18 18:07 UTC] uwendel at mysql dot com
Description:
------------
The PHP manual states that PDO->errorCode() returns the SQLSTATE associated with the last operation run on the handle, http://www.php.net/manual/en/function.PDO-errorCode.php 

"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).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-24 23:34 UTC] davidc@php.net
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] davidc@php.net
The patch below allows you to do:

$error = $pdo->errorCode();

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

http://dev.agoraproduction.com/php/5_3/ext/pdo/bug44153.patch


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] davidc@php.net
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
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2008-11-22 20:16 UTC] hradtke@php.net
Documentation has been updated.
 [2008-11-22 20:17 UTC] hradtke@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC