A production-oriented PDO helper for PHP applications that need a small, dependency-free database abstraction with prepared statements, transaction helpers, and predictable error handling.
- PDO-first API with prepared statements by default
- Works with MySQL/MariaDB and Microsoft SQL Server
- Safe defaults for modern workloads:
PDO::ERRMODE_EXCEPTIONPDO::ATTR_EMULATE_PREPARES = false(MySQL)- UTF-8 (
utf8mb4) MySQL charset by default
- Optional connection singleton keyed per connection config
- Transaction helpers (
beginTransaction,commit,rollBack,transaction) - Query execution helpers (
execute,fetch,fetchAll) - Optional query logger for observability and diagnostics
composer require a1phanumeric/php-mysql-class- PHP
>=7.4 ext-pdo- For MySQL:
ext-pdo_mysql - For SQL Server:
ext-sqlsrv
<?php
use A1phanumeric\DBPDO;
$db = new DBPDO('127.0.0.1', 'app_db', 'app_user', 'secret');
$user = $db->fetch('SELECT id, email FROM users WHERE id = ?', 42);$db->execute(
'UPDATE users SET email = ? WHERE id = ?',
['new-email@example.com', 42]
);$user = $db->fetch('SELECT * FROM users WHERE id = ?', 42);
if ($user === null) {
// Not found or query failed
}$users = $db->fetchAll('SELECT id, email FROM users WHERE status = ?', 'active');$usersByEmail = $db->fetchAll('SELECT id, email FROM users', null, 'email');$db->transaction(function (DBPDO $tx) {
$tx->execute('UPDATE accounts SET balance = balance - ? WHERE id = ?', [100, 1]);
$tx->execute('UPDATE accounts SET balance = balance + ? WHERE id = ?', [100, 2]);
});$db = DBPDO::getInstance('127.0.0.1', 'app_db', 'app_user', 'secret');$db = new DBPDO('127.0.0.1', 'app_db', 'app_user', 'secret', false, [
'persistent' => false,
'timeout' => 5,
'charset' => 'utf8mb4',
'port' => 3306,
]);SQL Server example:
$db = new DBPDO('sql.example.local', 'app_db', 'app_user', 'secret', true, [
'port' => 1433,
'encrypt' => true,
'trust_server_certificate' => false,
]);$db->setQueryLogger(function (string $query, array $params, ?float $durationMs, ?string $error) {
error_log(json_encode([
'query' => $query,
'params' => $params,
'duration_ms' => $durationMs,
'error' => $error,
]));
});If a query fails, inspect:
$lastError = $db->getLastError();execute(string $query, $values = null, bool $debug = false): PDOStatement|falsefetch(string $query, $values = null): ?arrayfetchAll(string $query, $values = null, ?string $key = null): arraytable_exists(string $table): boolbeginTransaction(): boolcommit(): boolrollBack(): boolinTransaction(): booltransaction(Closure $callback)lastInsertId(): stringgetPdo(): ?PDOgetLastError(): ?stringsetQueryLogger(callable $logger): self
- Existing constructor usage continues to work.
- Existing
execute,fetch, andfetchAllmethod names remain unchanged. execute(..., $debug = true)is deprecated and should be replaced withsetQueryLogger().- Singleton behavior is now safer for multi-database apps: each unique connection configuration gets its own instance.
- Always use placeholders with bound parameters for user input.
- Avoid persistent connections unless you have validated behavior under your workload.
- Enable TLS/secure transport for SQL Server (
encrypt => true) in production.
Packagist picks up new versions from Git tags.
- Update docs/changelog for the release.
- Commit and push to your default branch.
- Tag using semantic versioning.
git tag -a v3.0.0 -m "Release v3.0.0"
git push origin v3.0.0- Ensure your Packagist package auto-update webhook is configured, or trigger an update manually in Packagist.
Recommended versioning for these changes: major release (v3.0.0) for a clean SemVer boundary on the upgraded production behavior and API surface.
The legacy class is still included in this repository:
class.MySQL.phpclass.MySQL.README.md
MIT