PHP 8.4.1 Released!

pg_transaction_status

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

pg_transaction_statusReturns the current in-transaction status of the server

说明

pg_transaction_status(PgSql\Connection $connection): int

Returns the current in-transaction status of the server.

警告

pg_transaction_status() will give incorrect results when using a PostgreSQL 7.3 server that has the parameter autocommit set to off. The server-side autocommit feature has been deprecated and does not exist in later server versions.

参数

connection

PgSql\Connection 实例。

返回值

The status can be PGSQL_TRANSACTION_IDLE (currently idle), PGSQL_TRANSACTION_ACTIVE (a command is in progress), PGSQL_TRANSACTION_INTRANS (idle, in a valid transaction block), or PGSQL_TRANSACTION_INERROR (idle, in a failed transaction block). PGSQL_TRANSACTION_UNKNOWN is reported if the connection is bad. PGSQL_TRANSACTION_ACTIVE is reported only when a query has been sent to the server and not yet completed.

更新日志

版本 说明
8.1.0 现在 connection 参数接受 PgSql\Connection 实例,之前接受 resource

示例

示例 #1 pg_transaction_status() example

<?php
$dbconn
= pg_connect("dbname=publisher") or die("Could not connect");
$stat = pg_transaction_status($dbconn);
if (
$stat === PGSQL_TRANSACTION_UNKNOWN) {
echo
'Connection is bad';
} else if (
$stat === PGSQL_TRANSACTION_IDLE) {
echo
'Connection is currently idle';
} else {
echo
'Connection is in a transaction state';
}
?>

添加备注

用户贡献的备注 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