timefrontiers / php-sql-database
PHP SQL Database manager supporting MySQLi and PDO with backward compatibility
v1.0.0
2026-04-14 21:56 UTC
Requires
- php: >=8.1
- ext-mysqli: *
Requires (Dev)
- phpunit/phpunit: ^10.0
Suggests
- ext-pdo: For PDO database support
- ext-pdo_mysql: For MySQL PDO support
- ext-pdo_pgsql: For PostgreSQL PDO support
This package is auto-updated.
Last update: 2026-04-14 23:38:58 UTC
README
A flexible SQL database manager supporting MySQLi (default) and PDO with a unified interface.
Features
- Backward compatible – drop-in replacement for the legacy
MySQLDatabaseclass. - Dual backend support – MySQLi (default) or PDO via factory pattern.
- Prepared statements – both
MySQLiDatabaseandPDODatabasesupport secure parameterized queries. - Unified API – same method names across both drivers.
- Error collection – consistent with other TimeFrontiers packages.
Installation
composer require timefrontiers/php-sql-database
Requirements
- PHP 8.1 or higher
ext-mysqli(always required)ext-pdo+ driver (optional, for PDO support)
Basic Usage
Default: MySQLiDatabase
use TimeFrontiers\SQLDatabase; // This uses MySQLiDatabase internally (backward compatible) $db = new SQLDatabase('localhost', 'root', 'secret', 'my_database');
Using PDO Instead
use TimeFrontiers\SQLDatabase; use TimeFrontiers\PDODatabase; // Pass the PDO class name as the fourth parameter $db = new SQLDatabase('localhost', 'root', 'secret', PDODatabase::class, driver: 'mysql');
Executing Queries (Legacy Style)
$result = $db->query("SELECT * FROM users WHERE id = 1"); while ($row = $db->fetchAssocArray($result)) { // ... }
Using Prepared Statements (Recommended)
// Fetch all rows $users = $db->fetchAll("SELECT * FROM users WHERE status = ?", ['active']); // Fetch single row $user = $db->fetchOne("SELECT * FROM users WHERE id = ?", [5]); // Execute INSERT/UPDATE $db->execute("UPDATE users SET name = ? WHERE id = ?", ['John', 5]); $newId = $db->insertId();
License
MIT License. See LICENSE for details.