So the documentation on this function is pretty barebones (as is the case for a lot of thin PHP wrappers around C functions), but from what I've gathered by reading the libpq doc and trying various things, you should probably know the following :
* Polling the connection while the underlying socket is busy will cause the connection (or at least the polling, I'm not sure) to fail.
* As stated by the libpq documentation, "do not assume that the socket remains the same across PQconnectPoll calls"
* The socket will become ready after every change in connection status, so the connection must be polled multiple times until the function returns "polling_ok" or "polling_failed".
* "polling_active" can never be returned by libpq and has literally never been used anywhere ever, it has been an unused constant since day 1.
What you need to do is use pg_socket get a PHP stream object corresponding to the current socket and wait after it before polling, like so:
<?php
function pg_wait_connection_ready($conn) {
assert(is_resource($conn));
assert(get_resource_type($conn) === "pgsql link" || get_resource_type($conn) === "pgsql link persistent");
// "On the first iteration, i.e. if you have yet to call PQconnectPoll, behave as if it last returned PGRES_POLLING_WRITING."
$poll_outcome = PGSQL_POLLING_WRITING;
while (true) {
$socket = [pg_socket($conn)]; // "Caution: do not assume that the socket remains the same across PQconnectPoll calls."
$null = [];
if ($poll_outcome === PGSQL_POLLING_READING) {
stream_select($socket, $null, $null, 5);
$poll_outcome = pg_connect_poll($conn);
} else if ($poll_outcome === PGSQL_POLLING_WRITING) {
stream_select($null, $socket, $null, 5);
$poll_outcome = pg_connect_poll($conn);
} else {
break;
}
}
}
$db = pg_connect($conn_string, PGSQL_CONNECT_ASYNC);
// Do things while the connection is getting ready
pg_wait_connection_ready($db);
pg_query($sql);
?>
PHP.mk документација
pg_connect_poll
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
function.pg-connect-poll.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
function.pg-connect-poll.php
pg_connect_poll
Референца за `function.pg-connect-poll.php` со подобрена типографија и навигација.
pg_connect_poll
(PHP 5 >= 5.6.0, PHP 7, PHP 8)
pg_connect_poll — Проверете го статусот на тековниот асинхрон асинхрон обид за PostgreSQL конекција
= NULL
pg_connect_poll() ги проверува статусот на PostgreSQL конекцијата создадена со повикување pg_connect() со
PGSQL_CONNECT_ASYNC option.
Параметри
connection-
Еден PgSql\Connection instance.
Вратени вредности
Патеката до PHP скриптата што треба да се провери. PGSQL_POLLING_FAILED,
PGSQL_POLLING_READING,
PGSQL_POLLING_WRITING,
PGSQL_POLLING_OK, или
PGSQL_POLLING_ACTIVE.
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.1.0 |
На connection параметарот очекува PgSql\Connection
инстанца сега; претходно, а resource се очекуваше.
|
Белешки од корисници 1 белешка
VLroyrenn ¶
пред 7 години