Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Get MySQL error code when Exception is thrown
  • Hello there,

    Is there any way to get MySQL error number from Database_Exception?

    I know that there is method to get information like followings.

    $e->getCode();
    $e->getMessage();
    $e->getFile();
    $e->getLine();
    $e->getTrace();  

    But, none of them gets mysql error number.

    Thank you :)
  • I just found following.

    $e->getPrevious()->errorInfo

    Is this right way to get SQL error number?

  • It looks like it's not very consistently used in the code. For example, if connect failed, it says:

    Fuel\Core\Database_Exception [ 1044 ]:
    SQLSTATE[HY000] [1044] Access denied for user '**********'@'localhost' to database 'test'

    in which getCode() returns 1044, the MySQL code.

    Where exactly do you get your error?
  • For a query it seems to work fine too:

    Fuel\Core\Database_Exception [ 1146 ]:
    SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.table' doesn't exist with query...
  • Hello there,

    I am not getting error.

    I have following source code and I just want to handle duplicate entry error.

    try
    {
     $model = new \Model_User(); 
     $model->some_column = 'abc'; 
     $model->save();

     $model = new \Model_Class(); 
     $model->user_id = 100; // Set duplicate key, namely user_id must being unique key
     $model->save();
    }
    catch(\Database_Exception $e)
    {
     // Handle exception here
     if($e->getCode() == 1062){
       // Do something here
     }
    }

    Unfortunately, $e->getCode() gives me 23000 error, not 1062 error.

    Do you know why I am getting 23000 error?
  • What I get is following when exception is thrown.

    print_r($e->getMessage()); 
    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry....

    echo $e->getCode();
    23000
  • Can you not catch the exception, so you can see the backtrace and tell me which statement in which file generated the exception?
  • I got following.

    23000!

    Fuel\Core\Database_Exception [ 23000 ]:
    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '51' for key 'PRIMARY' with query: "INSERT INTO `user_classifications` (`user_id`, `classification_id`) VALUES ('51', '12')"

    COREPATH/classes/database/pdo/connection.php @ line 250

    245                            {
    246                                $error_code 0;
    247                            }
    248                        }
    249
    250                        throw new \Database_Exception($e->getMessage().' with query: "'.$sql.'"'$error_code$e);
    251                    }
    252                }
    253
    254                // no more attempts left, bail out
    255                else

    Backtrace

    1. COREPATH/classes/database/query.php @ line 314
    2. PKGPATH/orm/classes/query.php @ line 1505
    3. PKGPATH/orm/classes/model.php @ line 1390
    4. PKGPATH/orm/classes/model.php @ line 1336
    5. APPPATH/classes/model/user.php @ line 1206
    6. APPPATH/modules/general/classes/controller/user.php @ line 107
    7. COREPATH/classes/request.php @ line 473
    8. DOCROOT/index.php @ line 71
    9. DOCROOT/index.php @ line 96

    Prior Contents (show)

  • Thanks, I already see the issue, will look at it later today.
  • Have been thinking about this, but not sure this is a good idea, without something to keep the original PDO error code.

    Problem is that 23000 is a standard error, if you would swap MySQL by Oracle, the error will remain the same, so it can be caught. If that is changed to platform specific codes, you lose the platform independence of the PDO driver.

    So this needs a bit more thought, so you can access both.
  • HarroHarro
    Accepted Answer
    Addressed: https://github.com/fuel/core/commit/9d47bb390352729b410c729364ecec96d7c68b1c

    getCode() will continue to return the PDO SQLSTATE, a new getDbCode() method will return the underlying driver erorr code, if present.

    For non-PDO drivers, both will return the same code.
  • Hello there,

    Thanks for that.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion