how to catch pg_connect() function error?

前端 未结 2 1802
我在风中等你
我在风中等你 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:12

    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

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题