2

For example:

SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction ROLLING BACK

I get this message from a PDOEception thrown inside my code. I would like to intercept the error code (1213) and give it specific treatment.

Why?
For example deadlock means I just have to resubmit the query after a microsecond or so. Other errors, means I need to alert developers etc).

Right now I have to code (inside a class inheriting PDO):

    try{
        $this->lastStatement = $sql;
        $this->lastStatement->execute($params);
    }catch (PDOException $e){
        return $this->error($e);
    }

I can not use the getCode of $E as it seems to not have the total range of errors MySQL has. For example, the following error: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction will return HY000 Which is a very general code. Used on many different error types.

Should I parse the error message?

2
  • What about using $e->getCode()? Does it return anything meaningfull? Commented Nov 14, 2012 at 16:55
  • See answer below about getCode() Commented Nov 14, 2012 at 18:04

3 Answers 3

2
$e->getCode();

It contains the SQLSTATE code, where 40001 means "deadlock".

Sign up to request clarification or add additional context in comments.

2 Comments

Check this error which is lock related and I have to take care of the same way, but it gives the general error this time: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction
Yeah, that's more of a problem. I don't know any way of getting the more detailed error codes, besides "reading" the string programatically. Sorry.
0

Right now, The only choice I see is parsing the error message itself and extract the four digits error code.

$s='SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction';
$p='/\b[0-9]+/';
preg_match($p,$s,$m);
var_dump($m);

Comments

0

PDO Statements have a function ->errorInfo() which returns an array of useful info - check $err[2] where $err is the array returned from the above mentioned function.

http://php.net/manual/en/pdo.errorinfo.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.