©
本文档使用 PHP中文网手册 发布
(PHP 4 >= 4.2.0, PHP 5)
pg_connection_status — 获得连接状态
$connection
)pg_connection_status() 返回一个连接的状态。可能的状态为 PGSQL_CONNECTION_OK 和 PGSQL_CONNECTION_BAD。
Example #1 pg_connection_status() 例子
<?php
$dbconn = pg_connect ( "dbname=publisher" ) or die( "Could not connect" );
$stat = pg_connection_status ( $dbconn );
echo 'connection_status: ' . $stat ;
?>
Example #2 pg_connection_status() 例子
<?php
$dbconn = pg_connect ( "dbname=publisher" ) or die( "Could not connect" );
$stat = pg_connection_status ( $dbconn );
echo 'connection_status: ' . $stat ;
?>
参见 pg_connection_busy() 。
[#1] Mathieu De Zutter [2006-08-02 05:45:40]
Being careful with the comparison operators is not enough:
<?php
if (pg_connection_status($link)===PGSQL_CONNECTION_BAD)
reconnect($link);
?>
The reconnect won't be trigged when $link is null.
The manual is just wrong, there are three return values: PGSQL_CONNECTION_OK, PGSQL_CONNECTION_BAD, null
[#2] david dot tulloh at infaze dot com dot au [2005-06-14 20:33:01]
I think zytox is incorrect, at least in PHP 5.0.4.
It returns null, but you have to be careful with your comparison operators.
As an example:
<?php
unset($null);
if (pg_connection_status($null)===PGSQL_CONNECTION_OK)
echo 'this is not called';
if (pg_connection_status($null)==PGSQL_CONNECTION_OK)
echo 'this is called because NULL==0 is true';
?>
[#3] zytox at hotmail dot com [2005-04-02 10:34:04]
If the connection variable is NULL this function returns 0 in PHP 5.0.2. Havent figured out any more erratic values for the connection variable but be careful.