how to catch pg_connect() function error?

前端 未结 2 1801
我在风中等你
我在风中等你 2020-12-31 13:47

pg_connect() is showing the error in table format.Instead of showing error message as table format need a error message alert.

Error Message

2条回答
  •  耶瑟儿~
    2020-12-31 14:31

    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

提交回复
热议问题