如何捕获 pg_connect() 函数错误?
pg_connect() 以表格式显示错误。而不是以表格式显示错误消息,需要错误消息警报。
错误消息
警告:pg_connect() [function.pg-connect]:无法连接到 PostgreSQL 服务器:致命:第 41 行 /home/test/public_html/QueueManager/Modules/Database.php 中的用户“test”密码身份验证失败
if 之后将错误显示为表格格式。
执行 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();
}
}
请给我解决方案
pg_connect() is showing the error in table format.Instead of showing error message as table format need a error message alert.
Error Message
Warning: pg_connect() [function.pg-connect]: Unable to connect to PostgreSQL server: FATAL: password authentication failed for user "test" in /home/test/public_html/QueueManager/Modules/Database.php on line 41
After if showing error as table format.
After executing pg_connect() throwed exception.
But is is not working.
Code
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();
}
}
Please give me the solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
pg_connect
不会抛出异常,因此您必须像下面这样转换为异常。请参考此更多详细信息
http://php.net/manual/en/language.exceptions.php
pg_connect
does not throw exception, so you have to translate to exception like below.Please refer this more detail
http://php.net/manual/en/language.exceptions.php
要隐藏 PHP 生成的错误文本,请在函数调用前添加 @,例如:
More
To hide the error text generated by PHP, add @ in front of the function call, e.g.:
More details here