连接PostgreSQL数据库失败时返回连接错误状态

时间:2016-08-03 00:43:48

标签: php postgresql

我怎样才能编写以下php但使用postgresql函数而不是mysql?

if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

如果我无法连接到数据库,则输出错误。我会使用pg_result_error功能吗?

我相信它看起来像下面。如果我错了,请纠正我。将尽快测试验证。

if (pg_result_error_field($con))
{
    echo "Failed to connect to PostgreSQL: " . pg_result_error_field();
}

由于该字段不存在时此函数的返回值为NULL,或者失败时返回FALSE。以上代码应该有效,并且只有在pg_result_error_field函数返回值时才回显失败的响应。

文档:http://php.net/manual/en/function.pg-result-error-field.php

0 个答案:

没有答案