php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44154 PDO->errorInfo() does not always return three element array
Submitted: 2008-02-18 18:25 UTC Modified: 2008-11-22 20:18 UTC
From: uwendel at mysql dot com Assigned:
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:25 UTC] uwendel at mysql dot com
Description:
------------
According to the manual one can expect PDO->errorInfo() to return an array with three elements.

"Return Values

PDO->errorInfo() returns an array of error information about the last operation performed by this database handle. The array consists of the following fields:
Element 	Information
0 	SQLSTATE error code (a five-character alphanumeric identifier defined in the ANSI SQL standard).
1 	Driver-specific error code.
2 	Driver-specific error message."
http://www.php.net/manual/en/function.PDO-errorInfo.php

The manual is wrong. PDO will not always return an array with three elements. There are two situations when you will get an array with only one element.

  1) driver has not set an error code yet
  2) IM001 - not implemented (= driver can't set an error code)

I'd appreciate if you either document this or fix PDO->errorInfo() to always return an array with three elements. My personal preference is change PDO->errorInfo(). I don't see any reason why people would have to use a complicated test like the following to avoid "undefined index" style messages:

if (count($info = $pdo->errorInfo()) == 3)
  die(sprintf("SQLSTATE %s, Driver info: %s/%s", $info[0], $info[1], $info[2])); 
else
  die(sprintf("SQLSTATE %s", $info[0]));

Note that this bug is somewhat related to the question raised in http://bugs.php.net/bug.php?id=44153 . 



Reproduce code:
---------------
nixnutz@ulflinux:~/php53> sapi/cli/php -r '$pdo=new PDO("sqlite:/tmp/foo.db"); var_dump($pdo->errorCode()); var_dump($pdo->errorInfo());'
string(0) ""
array(1) {
  [0]=>
  string(0) ""
}

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

Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in Command line code on line 1
string(5) "IM001"
array(1) {
  [0]=>
  string(5) "IM001"
}


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

Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in Command line code on line 1
string(5) "IM001"
array(1) {
  [0]=>
  string(5) "IM001"
}

Expected result:
----------------
Always return an array with three elements. Make checking the errorInfo() easier.

Actual result:
--------------
See above. Array with only one element.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-01 13:52 UTC] davidc@php.net
I have a very small patch that I could commit if someone else agrees.

http://dev.agoraproduction.com/php/5_3/ext/pdo/bug44154.patch
 [2008-11-04 18:29 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.

Ok I have applied the patch to 5_3 and HEAD. 
 [2008-11-22 20:18 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 07:01:29 2024 UTC