php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51742 A non well formed numeric value encountered
Submitted: 2010-05-04 21:29 UTC Modified: 2015-06-06 14:49 UTC
Votes:16
Avg. Score:4.1 ± 0.9
Reproduced:16 of 16 (100.0%)
Same Version:7 (43.8%)
Same OS:9 (56.2%)
From: mike at mikegerwitz dot com Assigned: cmb (profile)
Status: Not a bug Package: PDO Core
PHP Version: 5.3.2 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
26 - 2 = ?
Subscribe to this entry?

 
 [2010-05-04 21:29 UTC] mike at mikegerwitz dot com
Description:
------------
PDOException extends RuntimeException which in turn extends Exception. The 
Exception class has the following constructor:

public __construct ([ string $message = "" [, int $code = 0 [, Exception 
$previous = NULL ]]] )

The $code parameter is to be of type integer. However, when using pdo_odbc or 
pdo_dblib, the error code returned by the server may be a string, rather than an 
integer. So, for example, calling $e->getCode() may yield a value of "12X34". 
This is inconsistent with the method definition as well:

final public int getCode ( void )

PHP's own internal library should not produce outcomes that are in conflict with 
PHP's definitions.

Test script:
---------------
try
{
    $pdo->query( 'BAD QUERY' );
}
catch ( PDOException $e )
{
    throw new Exception( $e->getMessage(), $e->getCode() );
}

Expected result:
----------------
// Just a thrown exception, nothing important

Actual result:
--------------
PHP Notice:  A non well formed numeric value encountered in [...] on line 7

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-17 13:46 UTC] mpartio at gmail dot com
I agree to Mike: PHP should allow non-numeric exception codes. I think this should be pretty easy to fix?
 [2011-04-25 18:35 UTC] bandy dot chris at gmail dot com
Related to bug 39615.
 [2014-01-01 12:49 UTC] felipe@php.net
-Package: PDO related +Package: PDO Core
 [2014-04-29 08:02 UTC] hanskrentel at yahoo dot de
PDOException violates the interface here to be able to use the SQLSTATE as code which is not necessarily an integer, but merely a string which might contain a non-numeric value.

Take note that this does not strictly violate the interface, as this has been solved by documenting in the interface that Exception::;getCode() might return a string:

http://www.php.net/manual/en/exception.getcode.php

So if you want to deal with this, you need to extend from Exception, override the constructor, save code as a private member and add the getter for it.
 [2014-04-29 08:07 UTC] hanskrentel at yahoo dot de
Here is some working example code, Excpetion::getCode() is final, however Exception::$code protected.


/**
 * Class PDOStatementException
 */
class PDOStatementException extends PDOException
{
    /**
     * @var string
     */
    private $statement;

    /**
     * @var string
     */
    protected $code;

    public static function createFrom(PDOException $PDOException, $statement) {

        if (!is_string($statement)) {
            throw new InvalidArgumentException(
                sprintf('Statement must be stirng, %s given', gettype($statement))
            );
        }

        $message  = $PDOException->getMessage() . " in statement '" . $statement . "'";
        $sqlstate = $PDOException->getCode();

        if (!is_string($sqlstate)) {
            throw new LogicException(
                sprintf('Expected type of PDOException::getCode() to be string, %s given', gettype($sqlstate))
            );
        }

        $exception = new PDOStatementException($message, 0, $PDOException);
        $exception->code = $sqlstate;
        $exception->statement = $statement;
        return $exception;
    }

    /**
     * @return string
     */
    public function getStatement()
    {
        return $this->statement;
    }
}
 [2015-06-06 14:49 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-06-06 14:49 UTC] cmb@php.net
I agree that the situation is somewhat unfortunate, but it is
documented that PDOException does not inherit a public
constructor, and that the return type of Exception::getCode() is
mixed.[1]

So this is not a bug, but rather a deliberate design decision.
FWIW, there's already request #39615 to allow strings as Exception
codes.

[1] <http://php.net/manual/en/class.pdoexception.php>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 19:01:31 2024 UTC