如何捕获 pg_connect() 函数错误?

发布于 2024-10-03 13:04:33 字数 1066 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可遇━不可求 2024-10-10 13:04:33

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();
}

请参考此更多详细信息

http://php.net/manual/en/language.exceptions.php

pg_connect does not throw exception, so you have to translate to exception like below.

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();
}

Please refer this more detail

http://php.net/manual/en/language.exceptions.php

焚却相思 2024-10-10 13:04:33

要隐藏 PHP 生成的错误文本,请在函数调用前添加 @,例如:

$conn = @pg_connect("host=$HOST dbname=$DBNAME user=$USER ".
                   "password=$PASSWORD sslmode=disable");

More

To hide the error text generated by PHP, add @ in front of the function call, e.g.:

$conn = @pg_connect("host=$HOST dbname=$DBNAME user=$USER ".
                   "password=$PASSWORD sslmode=disable");

More details here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文