Asynchronous SQLite 3 client for PHP based on Amp.
phenixphp/sqlite is part of the Phenix PHP framework ecosystem. Phenix is a web framework built on pure PHP, without external extensions, based on the Amphp ecosystem, which provides non-blocking operations, asynchronism and parallel code execution natively. It runs in the PHP SAPI CLI and on its own server — it is simply powerful.
| Requirement | Version |
|---|---|
| PHP | ^8.2 |
| ext-sqlite3 | * |
Install the package via Composer:
composer require phenixphp/sqliteThe SqliteConfig class is the entry point for configuring the SQLite connection. It allows you to define the database path and connection parameters aligned with the Amphp SQL ecosystem.
<?php
use Phenix\Sqlite\SqliteConfig;
$config = new SqliteConfig(
database: __DIR__ . '/database.db',
);You can use
:memory:as the database path to create an in-memory SQLite database, which is ideal for testing scenarios.
$config = new SqliteConfig(
database: ':memory:',
);Use SqliteConfig to create and manage asynchronous connections to your SQLite database. Since this package is built on top of Amphp, all database operations are non-blocking and run asynchronously using fibers.
<?php
declare(strict_types=1);
use Phenix\Sqlite\SqliteConfig;
use function Phenix\Sqlite\connect;
$config = new SqliteConfig(
database: __DIR__ . '/database.db',
);
$connection = connect($config);
// ... use connection ...
$connection->close();Once you have an active connection, you can execute SQL statements — CREATE, INSERT, SELECT, UPDATE, and DELETE — all asynchronously without blocking the event loop.
$connection->execute(
'CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL
)'
);$connection->execute(
"INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')"
);$result = $connection->query('SELECT * FROM users');
foreach ($result as $row) {
echo $row['id'] . ': ' . $row['name'] . ' — ' . $row['email'] . PHP_EOL;
}$connection->execute(
"UPDATE users SET name = 'Jane Doe' WHERE id = 1"
);$connection->execute(
"DELETE FROM users WHERE id = 1"
);Prepared statements allow you to precompile a SQL template and execute it repeatedly with different bound parameters. This improves both performance and security by preventing SQL injection.
$statement = $connection->prepare(
'INSERT INTO users (name, email) VALUES (?, ?)'
);
$statement->execute(['Alice', 'alice@example.com']);
$statement->execute(['Bob', 'bob@example.com']);$statement = $connection->prepare(
'SELECT * FROM users WHERE name = ? AND email = ?'
);
$result = $statement->execute(['Alice', 'alice@example.com']);
foreach ($result as $row) {
echo $row['name'] . PHP_EOL;
}$statement->close();Transactions group multiple SQL operations into a single atomic unit. If any operation fails, all changes within the transaction are rolled back, ensuring data consistency.
$transaction = $connection->beginTransaction();
try {
$transaction->execute(
"INSERT INTO users (name, email) VALUES ('Charlie', 'charlie@example.com')"
);
$transaction->execute(
"INSERT INTO users (name, email) VALUES ('Diana', 'diana@example.com')"
);
$transaction->commit();
} catch (\Throwable $e) {
$transaction->rollback();
throw $e;
}$transaction = $connection->beginTransaction();
try {
$statement = $transaction->prepare(
'INSERT INTO users (name, email) VALUES (?, ?)'
);
$statement->execute(['Eve', 'eve@example.com']);
$statement->execute(['Frank', 'frank@example.com']);
$transaction->commit();
} catch (\Throwable $e) {
$transaction->rollback();
throw $e;
}You can learn about Phenix in the official documentation and discover the power of asynchronous and concurrent applications in native PHP.
If you discover a security vulnerability within Phenix, please send an e-mail to Omar Barbosa via contacto@omarbarbosa.com. All security vulnerabilities will be promptly addressed.
This package is open-sourced software licensed under the MIT license.