php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41064 PDOException->getCode() returns string
Submitted: 2007-04-12 15:54 UTC Modified: 2007-04-12 18:19 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: p dot volk at veb-it dot de Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.2.1 OS: Linux 2.6.18-4-k7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: p dot volk at veb-it dot de
New email:
PHP Version: OS:

 

 [2007-04-12 15:54 UTC] p dot volk at veb-it dot de
Description:
------------
PDOException->getCode() returns string instead integer.
I've tested this code with php 5.2.1 and PHP 5.2.2RC2-dev (cli) (built: Apr 12 2007 17:17:02), from php5.2-200704121430.tar.bz2.

Reproduce code:
---------------
<?php
error_reporting(8191);
$db = new PDO('mysql:dbname=testdb;host=ahost', 'me', 'phpRulez');
#$db = new PDO('pgsql:dbname=testdb;host=ahost', 'me', 'phpRulez');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = array(array('php', 'rulez'), array('php', 'rulez'));
$stmt = $db->prepare('INSERT INTO atable VALUES (?, ?)');
$stmt->bindParam(1, $lang, PDO::PARAM_STR);
$stmt->bindParam(2, $action, PDO::PARAM_STR);
foreach($data as $set) {
    $lang = $set[0];
    $action = $set[1];
    try { $stmt->execute(); }
    catch (PDOException $e) {
        if ($e->getCode() === 23000 || 23505 === $e->getCode())
            continue;
        else var_dump($e->getCode()); print $e->getMessage()."\n";
    }
}
$db = null;
?>

Expected result:
----------------
No Output AND PDOException->getCode() returns an integer.

Actual result:
--------------
* PsotgreSQL
string(5) "23505"
SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key violates unique constraint "pkey_atable"

* MySQL
string(5) "23000"
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'php-rulez' for key 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-12 16:02 UTC] tony2001@php.net
Expected behaviour.
 [2007-04-12 16:21 UTC] p dot volk at veb-it dot de
This is bug, it is'n bogus. Have a look at this code and it's ouput:



pvo@foo tmp $ cat testcode.php && php testcode.php 
<?php
error_reporting(8191);
try {
    $db = new PDO('mysql:dbname=testdb;host=ahost');
}
catch (PDOException $e) {
    var_dump($e->getCode());
}
throw new Exception('hello', 'text');
?>
int(0)

Fatal error: Wrong parameters for Exception([string $exception [, long $code ]]) in /tmp/testcode.php on line 9
 [2007-04-12 16:38 UTC] tony2001@php.net
PDOException and Exception are different things.
PDOException has to be able to store codes like HYT00 or XX000, which are clearly not integers.
 [2007-04-12 18:14 UTC] p dot volk at veb-it dot de
A fine excuse!
But <?php Reflection::export(new ReflectionClass('PDOException')); ?> tells me amongst others:
'Class [ <internal:PDO> class PDOException extends RuntimeException ] ...'
If PDOException would really extend RuntimeException/Exception, how is it possible, that the attribute code is a string?

If I try to extend the class Exception with a own Exception class and the attribute code isn't an integer. I'll get inevitably a fatal error.

The documentation at http://de.php.net/pdo, section 'Errors and error handling'/PDO::ERRMODE_EXCEPTION mentions 'see Exceptions for more information about Exceptions in PHP.', but nothing about the type casting.

Well, why don't you extend Exception correctly, with an attribute sqlstate and a method getSQLState()?
 [2007-04-12 18:19 UTC] tony2001@php.net
It is done this way for a good reason, which I've already explained.
There is no bug, that's how it is supposed to work.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 08:01:29 2024 UTC