我有一段代码,它将
我的代码是针对一个PostgreSQL数据库运行的。使用以下DDL:
请使用以下代码来重现我的问题(替换为您自己的PostgeSQL连接值):
在我的输出中,最终输出中缺少
在端口号不正确时,抛出PDOException的示例中,序列化的
PDOException
序列化后发送到网络中,在稍后的时间内再进行反序列化。当我进行反序列化时,$code
属性似乎已经丢失了,但对象的其余部分没有变化。我的代码是针对一个PostgreSQL数据库运行的。使用以下DDL:
CREATE TABLE test (
id INTEGER
);
请使用以下代码来重现我的问题(替换为您自己的PostgeSQL连接值):
<?php
$dsn = "pgsql: dbname=postgres;host=/var/run/postgresql;port=5432";
$user = "postgres";
$password = "";
try
{
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$res = $pdo->exec("INSERT INTO test (id) VALUES (999999999999999)");
}
catch (PDOException $e)
{
var_dump((array) $e);
print "\n";
print $e->getCode();
print "\n";
$s = serialize($e);
print $s;
print "\n";
$d = unserialize($s);
var_dump((array) $d);
print "\n";
print $d->getCode();
print "\n";
print serialize($e->getCode());
print "\n";
}
?>
在我的输出中,最终输出中缺少
$code
属性。此外,我收到以下通知:PHP Notice: Undefined property: PDOException::$code in /home/developer/test_serialize.php on line 20
。我发现实际上必须执行失败的SQL语句才能看到这个问题。特别是如果我选择了错误的端口号,那么我将得到一个PDOException
,但在unserialize
调用后它仍然保留$code
属性。请注意,序列化字符串似乎有代码属性存在,因此我认为这是unserialize
函数的问题。任何见解都会被赞赏 - 我是否对某些基本概念有误解?这是PHP的错误吗?还是其他原因?我使用以下PHP版本:PHP 7.1.6 (cli) (built: Jun 18 2018 12:25:10) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
编辑 - 添加打印输出
以下是复制脚本的输出。请注意,我稍微修改了一些内容以添加一些换行符以增强可读性,并将print_r
替换为var_dump
:
array(8) {
["*message"]=>
string(75) "SQLSTATE[22003]: Numeric value out of range: 7 ERROR: integer out of range"
["Exceptionstring"]=>
string(0) ""
["*code"]=>
string(5) "22003"
["*file"]=>
string(34) "/home/developer/test_serialize.php"
["*line"]=>
int(10)
["Exceptiontrace"]=>
array(1) {
[0]=>
array(6) {
["file"]=>
string(34) "/home/developer/test_serialize.php"
["line"]=>
int(10)
["function"]=>
string(4) "exec"
["class"]=>
string(3) "PDO"
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
string(73) "INSERT INTO km_role (role_id, role_name) VALUES (999999999999999, 'test')"
}
}
}
["Exceptionprevious"]=>
NULL
["errorInfo"]=>
array(3) {
[0]=>
string(5) "22003"
[1]=>
int(7)
[2]=>
string(28) "ERROR: integer out of range"
}
}
22003
O:12:"PDOException":8:{s:10:"*message";s:75:"SQLSTATE[22003]: Numeric value out of range: 7 ERROR: integer out of range";s:17:"Exceptionstring";s:0:"";s:7:"*code";s:5:"22003";s:7:"*file";s:34:"/home/developer/test_serialize.php";s:7:"*line";i:10;s:16:"Exceptiontrace";a:1:{i:0;a:6:{s:4:"file";s:34:"/home/developer/test_serialize.php";s:4:"line";i:10;s:8:"function";s:4:"exec";s:5:"class";s:3:"PDO";s:4:"type";s:2:"->";s:4:"args";a:1:{i:0;s:73:"INSERT INTO km_role (role_id, role_name) VALUES (999999999999999, 'test')";}}}s:19:"Exceptionprevious";N;s:9:"errorInfo";a:3:{i:0;s:5:"22003";i:1;i:7;i:2;s:28:"ERROR: integer out of range";}}
array(7) {
["*message"]=>
string(75) "SQLSTATE[22003]: Numeric value out of range: 7 ERROR: integer out of range"
["Exceptionstring"]=>
string(0) ""
["*file"]=>
string(34) "/home/developer/test_serialize.php"
["*line"]=>
int(10)
["Exceptiontrace"]=>
array(1) {
[0]=>
array(6) {
["file"]=>
string(34) "/home/developer/test_serialize.php"
["line"]=>
int(10)
["function"]=>
string(4) "exec"
["class"]=>
string(3) "PDO"
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
string(73) "INSERT INTO km_role (role_id, role_name) VALUES (999999999999999, 'test')"
}
}
}
["Exceptionprevious"]=>
NULL
["errorInfo"]=>
array(3) {
[0]=>
string(5) "22003"
[1]=>
int(7)
[2]=>
string(28) "ERROR: integer out of range"
}
}
PHP Notice: Undefined property: PDOException::$code in /home/developer/test_serialize.php on line 24
s:5:"22003"
在端口号不正确时,抛出PDOException的示例中,序列化的
$e->getCode()
为:i:7;
PDOException
前添加反斜杠,即\PDOException
。 - Lovepreet Singhserialize($e->getCode());
的输出结果。 - jh1711