php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61040 PDOException::getCode() returns SQLSTATE instead of error code
Submitted: 2012-02-10 11:22 UTC Modified: 2012-03-05 06:07 UTC
From: jjgmeijer at hotmail dot com Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.3SVN-2012-02-10 (SVN) OS: Linux
Private report: No CVE-ID: None
 [2012-02-10 11:22 UTC] jjgmeijer at hotmail dot com
Description:
------------
PDOException::getCode() returns the SQLSTATE instead of the error code.

For example a var_dump of a random PDOException:

object(PDOException)[97]
  protected 'message' => string 'SQLSTATE[40001]: Serialization failure: 1213 
Deadlock found when trying to get lock; try restarting transaction' (length=111)
  private 'string' (Exception) => string '' (length=0)
  protected 'code' => string '40001' (length=5)

then the 'code' should not be the SQLSTATE (40001) but the error code (1213). My 
reason for this is that the SQLSTATE may not be unique, the error code is.

For more info: http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html

Test script:
---------------
Try to get a random PDOException with an invalid query.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-05 06:07 UTC] rasmus@php.net
But the error code is not portable in any way and PDO tries to normalize the 
interface to the databases, so I think returning the SQLSTATE is more useful here 
from a portability perspective. The error code is still shown as part of 
getMessage() and you have it separated out in PDOException::errorInfo where you 
get the SQLSTATE, the error code and the error text message in 3 separate 
elements so if your app needs to dig beyond the standard ANSI SQL codes it can 
quite easily.
 [2012-03-05 06:07 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 00:01:41 2024 UTC