The LibSQLTransaction class represents a database transaction in the LibSQL PHP Extension. It provides methods to manage database transactions, execute SQL statements within transactions, and retrieve transaction details.

Class Synopsis

class LibSQLTransaction
{
    // Methods
    public function __construct(string $conn_id, string $trx_mode);
    public function changes(): int;
    public function isAutocommit(): bool;
    public function exec(string $stmt, array $parameters = []): int;
    public function query(string $stmt, array $parameters = []): array;
    public function commit(): void;
    public function rollback(): void;
}

Table of Contents

Description

  • __construct(string connid,stringconn_id, string trx_mode): Construct by LibSQL::transaction().

  • changes(): int: Returns the number of rows changed by the last SQL statement executed within the transaction.

  • isAutocommit(): bool: Checks if the transaction is set to autocommit.

  • exec(string stmt,arraystmt, array parameters = []): int: Executes an SQL statement within the transaction with optional parameters and returns the number of affected rows.

  • query(string stmt,arraystmt, array parameters = []): array: Executes a query within the transaction with optional parameters and returns the result set as an array.

  • commit(): void: Commits the transaction, making all changes permanent.

  • rollback(): void: Rolls back the transaction, undoing all changes made within the transaction.

Example

// Create a new LibSQL instance
$db = new LibSQL("database.db");

$tx = $db->transaction();

$tx->exec("INSERT INTO users (name, age) VALUES (:name, :age)", [
    ":name" => "Soimah Pancawati",
    ":age" => "37"
]);

if (false) {
    $tx->commit();
} else {
    $tx->rollback();
}

Notes

Ensure proper error handling and transaction management when using the LibSQLTransaction class to handle potential errors and ensure data integrity.