odbc_errormsg
不按照应该的方式报告odbc_execute
的错误消息。它只是发出警告。所以我不得不通过error_get_last
写一个黑客来解析错误信息。error_get_last()和自定义错误处理程序
我使用set_error_handler
和error_get_last
回报NULL
,除非我可以:
禁用我的错误处理程序,
或使其返回
FALSE
。
我想这是由于PHP的内置错误处理程序负责将错误细节存储在某处,以便稍后可以检索它们。
有没有办法在我的自定义错误处理程序中模拟这种行为,所以error_get_last()
可以正常使用?
请注意我已经知道几种方法随时检索错误信息。我的问题是如何使error_get_last
可用。
更新:我想我应该张贴一些代码。
PHP有error_get_last()
,这使得要做到这一点:
@fopen('xxx');
var_dump(error_get_last());
...并得到这样的:
array(4) {
["type"]=>
int(2)
["message"]=>
string(46) "fopen() expects at least 2 parameters, 1 given"
["file"]=>
string(69) "C:\Documents and Settings\ALVARO.GONZALEZ\Mis documentos\tmp\test.php"
["line"]=>
int(3)
}
如果更换内置的错误处理这打破:
function custom_error_handler($errno, $errstr, $errfile, $errline){
$ignore = ($errno & error_reporting()) == 0;
if(!$ignore){
echo "[Error happened: $errstr]\n";
}
return TRUE;
}
set_error_handler('custom_error_handler');
@fopen('xxx');
var_dump(error_get_last()); // NULL
如果您保留两个错误处理程序...
function custom_error_handler($errno, $errstr, $errfile, $errline){
$ignore = ($errno & error_reporting()) == 0;
if(!$ignore){
echo "[Error happened: $errstr]\n";
}
return FALSE;
}
set_error_handler('custom_error_handler');
error_reporting(E_ALL);
echo $foo;
...你得到的副作用:
[Error happened: Undefined variable: foo]
Notice: Undefined variable: foo in C:\Documents and Settings\ALVARO.GONZALEZ\Mis documentos\tmp\test.php on line 15
Call Stack:
0.0004 329720 1. {main}() C:\Documents and Settings\ALVARO.GONZALEZ\Mis documentos\tmp\test.php:0
...而不是只是:
[Error happened: Undefined variable: foo]
我想我的自定义错误处理程序与error_get_last
正确连接。我想error_get_last
工作正常。
您是否接受使用不同的(用户定义的)功能?因为如果你愿意,你可以将最后一个错误存储在一个全局变量中,并且有一个函数,它只是返回$ GLOBALS ['varname'];' – DaveRandom 2011-12-14 12:17:23
返回'false'是不可接受的?如果是这样,为什么? – Jon 2011-12-14 12:20:18