I'm converting a PHP code, which uses mysqli for connecting to mysql database, to a PHP code that uses PDO for connecting to mysql database.
Is there a SQLSTATE which is equivalent to the error code 2006 in mysqli?
Because a piece of my code is written like this:
switch($this->_dbi->errno){
case 2006:
$this->close();
throw new Exception();
break;
}
How can I use PDO to rewrite this code?
SQLSTATE is the return value of PDO::errorCode.
Error 2006(CR_SERVER_GONE_ERROR) means MySQL server has gone away
You can use the array of PDO::errorInfo
. There you get an array like the following:
Array
(
[0] => HY000
[1] => 1
[2] => near "bogus": syntax error
)
PDO::errorCode
.Your example would look like this:
switch ($pdo->errorInfo()[1]) {
case 2006:
$this->close();
throw new Exception();
break;
}