pg_connect()以表格格式显示错误.而不是显示错误消息,因为表格格式需要错误消息警报.
错误消息
警告:pg_connect()[function.pg-connect]:无法连接到PostgreSQL服务器:致命错误:第41行/home/test/public_html/QueueManager/Modules/Database.php中的用户"test"密码验证失败
如果以表格格式显示错误.
执行pg_connect()后抛出异常.
但是不行.
码
function connect()
{
$HOST = $GLOBALS[Database_Conn][Db_Host]; # Host name
$USER = $GLOBALS[Database_Conn][Db_User]; # database user name
$DBNAME = $GLOBALS[Database_Conn][Db_Name]; # name of the database
$PASSWORD = $GLOBALS[Database_Conn][Db_Pass]; # password the database user.
try
{
$conn = pg_connect("host=$HOST dbname=$DBNAME user=$USER ".
"password=$PASSWORD sslmode=disable");
if(!$conn)
{
throw new Exception("Database Connection Error");
}
return $conn;
}
catch (Exception $e)
{
print <<<_HTML_
<script> alert('Caught exception');
</script> _HTML_;
die();
}
}
Run Code Online (Sandbox Code Playgroud)
请给我解决方案
小智 18
pg_connect 不抛出异常,所以你必须转换为异常,如下所示.
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");
try {
$conn=@pg_connect("host=dbhost user=dbuser dbname=db password=dbpass");
} Catch (Exception $e) {
Echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
请参考这个更多细节
http://php.net/manual/en/language.exceptions.php
要隐藏 PHP 生成的错误文本,请在函数调用前添加 @,例如:
$conn = @pg_connect("host=$HOST dbname=$DBNAME user=$USER ".
"password=$PASSWORD sslmode=disable");
Run Code Online (Sandbox Code Playgroud)
更多细节在这里