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
pg_connection_status
(PHP 4 >= 4.2.0, PHP 5)
pg_connection_status — 接続ステータスを取得する
説明
int pg_connection_status
( resource $connection
)
pg_connection_status() は、 connection で指定した接続のステータスを返します。
パラメータ
- connection
-
PostgreSQL データベースの接続リソース。
返り値
PGSQL_CONNECTION_OK あるいは PGSQL_CONNECTION_BAD 。
例
例1 pg_connection_status() の例
<?php
$dbconn = pg_connect("dbname=publisher") or die("Could not connect");
$stat = pg_connection_status($dbconn);
if ($stat === PGSQL_CONNECTION_OK) {
echo 'Connection status ok';
} else {
echo 'Connection status bad';
}
?>
pg_connection_status
Mathieu De Zutter
02-Aug-2006 12:45
02-Aug-2006 12:45
david dot tulloh at infaze dot com dot au
15-Jun-2005 03:33
15-Jun-2005 03:33
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';
?>
zytox at hotmail dot com
02-Apr-2005 06:34
02-Apr-2005 06:34
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.
