PHP 8.5.0 Alpha 1 available for testing

pg_transaction_status

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

pg_transaction_status Retorna el estado de la transacción en curso del servidor

Descripción

pg_transaction_status(PgSql\Connection $connection): int

Retorna el estado de la transacción en curso del servidor.

Precaución

pg_transaction_status() proporcionará resultados incorrectos cuando se utilice con un servidor PostgreSQL 7.3 que tenga el parámetro autocommit desactivado. La funcionalidad de autocommit está obsoleta y ya no existe en las versiones más recientes del servidor.

Parámetros

connection

An PgSql\Connection instance.

Valores devueltos

El estado puede ser PGSQL_TRANSACTION_IDLE (actualmente inactivo), PGSQL_TRANSACTION_ACTIVE (una orden está en curso), PGSQL_TRANSACTION_INTRANS (inactivo, dentro de un bloque de transacción válido), o PGSQL_TRANSACTION_INERROR (inactivo, dentro de un bloque de transacción fallido). PGSQL_TRANSACTION_UNKNOWN se retorna si la conexión es incorrecta. PGSQL_TRANSACTION_ACTIVE se retorna solo si la consulta ha sido enviada al servidor y esta aún no ha sido completada.

Historial de cambios

Versión Descripción
8.1.0 The connection parameter expects an PgSql\Connection instance now; previously, a recurso was expected.

Ejemplos

Ejemplo #1 Ejemplo con pg_transaction_status()

<?php
$dbconn
= pg_connect("dbname=publisher") or die("Conexión imposible");
$stat = pg_transaction_status($dbconn);
if (
$stat === PGSQL_TRANSACTION_UNKNOWN) {
echo
'Conexión incorrecta';
} else if (
$stat === PGSQL_TRANSACTION_IDLE) {
echo
'Conexión actualmente inactiva';
} else {
echo
'Conexión está en curso de transacción';
}
?>

add a note

User Contributed Notes 2 notes

up
0
r dot grellmann at agentmulder dot de
3 years ago
After one of the asynchronous functions (pg_send_query(), pg_send_query_params()...) has been used, pg_transaction_status() will always report PGSQL_TRANSACTION_ACTIVE, no matter if a transaction was started or not.
Even after the first call (or all valid calls) to pg_get_result() the transaction status will stay PGSQL_TRANSACTION_ACTIVE, until either
- a synchronous function like pq_query() was used, or
- another call to pg_get_result() was made, which returns false.
Note: calling pg_free_result() does not change anything.

<?php
$conn
= pg_connect("dbname=publisher") or die("Could not connect");
$status = pg_transaction_status($conn); // PGSQL_TRANSACTION_IDLE
pg_send_query($conn, 'SELECT 1');
$status = pg_transaction_status($conn); // PGSQL_TRANSACTION_ACTIVE
$res = pg_get_result($conn);
$status = pg_transaction_status($conn); // PGSQL_TRANSACTION_ACTIVE
$res = pg_get_result($conn); // false
$status = pg_transaction_status($conn); // PGSQL_TRANSACTION_IDLE
?>
up
0
btherl at yahoo dot com dot au
18 years ago
This function is implemented in C, so there's no way to mimic it in SQL for older versions of PHP. But you can mimic some of the functionality by using a wrapper which keeps track of when you begin and commit/rollback transactions.
To Top