Jump to content
NotionCommotion

Get specific MySQL/MariaDB error info

Recommended Posts

I wish to respond specifically to PDO errors.  $e->getMessage() shows something like the following:

  • SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails...
  • SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ...

I suppose I can just look for 1452 and 1062, but thought using http://php.net/manual/en/pdo.errorcode.php would be better, but it is empty.  I am using Server version: 10.2.18-MariaDB MariaDB Server.  Any suggestions?

Thanks

try{
    $this->pdo->prepare($sql)->execute([$value, $id, $this->accountsId]);
}
catch(\PDOException $e) {
    syslog(LOG_ERR, json_encode($this->pdo->errorCode()));
    syslog(LOG_ERR, json_encode($this->pdo->errorInfo()));
}
"00000"
["00000",null,null]


 

Share this post


Link to post
Share on other sites

The error message could not be any more clear. You are attempting to insert/update/delete a column with a duplicate constraint with data that is already in the DB and incorrectly messing with data in a parent table that has child data.

Edited by benanamen

Share this post


Link to post
Share on other sites
4 minutes ago, benanamen said:

The error message could not be any more clear. You are attempting to insert/update/delete a column with a duplicate constraint with data that is already in the DB

I have no issues with the clarity of the error message.  Why does PDO::errorCode() and PDO::errorInfo() provide no content?

Share this post


Link to post
Share on other sites

I don't understand what you are looking for. What "content" do you want? Are you asking how to handle the error? If so, you can try/catch the error and respond the way you want.

From the manual

Quote

PDO::errorCode() only retrieves error codes for operations performed directly on the database handle. If you create a PDOStatement object through PDO::prepare() or PDO::query() and invoke an error on the statement handle, PDO::errorCode() will not reflect that error.

Source: http://php.net/manual/en/pdo.errorcode.php

Edited by benanamen
  • Like 1

Share this post


Link to post
Share on other sites
5 minutes ago, Barand said:

Perhaps there is something here that might help

I don't think this will help determine whether a duplicate or fk issue.

Share this post


Link to post
Share on other sites

Oh yes it will

try {
    $db->exec("INSERT INTO units(id, unit) VALUES (1, 'Test')");      // deliberate duplicate 
}
catch (PDOException $e) {
    echo '<pre>', print_r($e->errorInfo, 1), '</pre>';
}

Outputs

Array
(
    [0] => 23000
    [1] => 1062
    [2] => Duplicate entry '1' for key 'PRIMARY'
)

 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.