2006年从mysqli迁移到PDO的错误代码映射
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
我正在将使用mysqli连接到mysql数据库的PHP代码转换为使用PDO的PHP代码 用于连接到mysql数据库。 p>
是否有 SQLSTATE ,相当于错误代码 2006 ? p>
因为我的代码是这样编写的: p>
switch($ this-> _dbi-> errno){
case 2006:
$ this-> close();
throw new Exception();
break;
}
code > pre>
如何使用PDO重写此代码? p>
SQLSTATE em>是 PDO :: errorCode 。 p>
错误2006(CR_SERVER_GONE_ERROR) 意味着 MySQL服务器已经消失 em> p>
div>
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
)
-
0: This is the same value like on
PDO::errorCode
. - 1: The error number. Different between the database providers, but here would be your error number 2006.
- 2: The error message / description.
Your example would look like this:
switch ($pdo->errorInfo()[1]) {
case 2006:
$this->close();
throw new Exception();
break;
}