solophp / query-executor
A lightweight PDO wrapper for executing raw SQL queries with parameter binding and flexible result handling.
v1.0.0
2025-04-25 19:09 UTC
Requires
- php: >=8.2
- ext-pdo: *
README
A lightweight PDO wrapper for executing raw SQL queries with parameter binding and flexible result handling.
Features
- Simple, fluent API for database operations
- Secure parameter binding for query execution
- Transaction management with automatic rollback on exceptions
- Configurable fetch modes
- Type-safe return values with PHP 8's strict typing
- Minimalist design with zero dependencies
Installation
composer require solophp/query-executor
Quick Start
use Solo\QueryExecutor; use Solo\QueryExecutor\Config; use Solo\QueryExecutor\Connection; // Create configuration $config = new Config( host: 'localhost', user: 'root', pass: 'password', db: 'my_database' ); // Create query executor $db = new QueryExecutor(new Connection($config)); // Query with parameters $user = $db->query('SELECT * FROM users WHERE id = ?', [1])->fetch(); // Insert data and get ID $db->query('INSERT INTO users (name, email) VALUES (?, ?)', ['John', 'john@example.com']); $lastId = $db->lastInsertId(); // Transaction support $db->withTransaction(function(QueryExecutor $executor) { $executor->query('UPDATE accounts SET balance = balance - ? WHERE id = ?', [100, 1]); $executor->query('UPDATE accounts SET balance = balance + ? WHERE id = ?', [100, 2]); // Automatic rollback on exception });
API Reference
QueryExecutor Methods
query(string $sql, array $bindings = []): self
- Execute a SQL query with parameter bindingsfetchAll(?int $mode = null): array
- Fetch all rows from the result setfetch(?int $mode = null): array|stdClass|null
- Fetch a single row from the result setfetchColumn(int $idx = 0): mixed
- Fetch a single column from the result setlastInsertId(): string|false
- Get the last inserted IDrowCount(): int
- Get the number of affected rowsbeginTransaction(): bool
- Begin a transactioncommit(): bool
- Commit a transactionrollBack(): bool
- Roll back a transactionwithTransaction(callable $callback): mixed
- Execute a callback within a transaction
License
MIT