PHP 8.4.1 Released!

pg_connection_status

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

pg_connection_status Определяет состояние подключения

Описание

pg_connection_status(PgSql\Connection $connection): int

pg_connection_status() возвращает состояние переданного в качестве аргумента соединения connection.

Список параметров

connection

Экземпляр класса PgSql\Connection.

Возвращаемые значения

PGSQL_CONNECTION_OK либо PGSQL_CONNECTION_BAD.

Список изменений

Версия Описание
8.1.0 Параметр connection теперь ожидает экземпляр класса PgSql\Connection; раньше параметр ожидал ресурс (resource).

Примеры

Пример #1 Пример использования pg_connection_status()

<?php
$dbconn
= pg_connect("dbname=publisher") or die("Не удалось соединиться");
$stat = pg_connection_status($dbconn);
if (
$stat === PGSQL_CONNECTION_OK) {
echo
'Статус соединения: доступно';
} else {
echo
'Статус соединения: разорвано';
}
?>

Смотрите также

  • pg_connection_busy() - Проверяет, занято ли соединение в данный момент.

Добавить

Примечания пользователей 4 notes

up
9
Mathieu De Zutter
18 years ago
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
up
3
zytox at hotmail dot com
19 years ago
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.
up
1
Anonymous
6 years ago
The wiki page doesn't currently reflect that, but if you're passing PGSQL_CONNECT_ASYNC to pg_connect, creating the connection won't block and transitions through extra states expressed by those constants.

PGSQL_CONNECTION_AUTH_OK
PGSQL_CONNECTION_AWAITING_RESPONSE
PGSQL_CONNECTION_MADE
PGSQL_CONNECTION_SETENV
PGSQL_CONNECTION_SSL_STARTUP
PGSQL_CONNECTION_STARTED
up
0
david dot tulloh at infaze dot com dot au
19 years ago
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';
?>
To Top