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

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.