Jump to content

Get specific MySQL/MariaDB error info


NotionCommotion

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]


 

Link to comment
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?

Link to comment
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

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

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.