Simple MySQL class written in PHP, for interfacing with a MySQL database.
Simple MySQL class written in PHP, for interfacing with a MySQL database.
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::ERRMODE_EXCEPTIONPDO::ATTR_EMULATE_PREPARES = false (MySQL)utf8mb4) MySQL charset by defaultbeginTransaction, commit, rollBack, transaction)execute, fetch, fetchAll)composer require a1phanumeric/php-mysql-class>=7.4ext-pdoext-pdo_mysqlext-sqlsrvfetch('SELECT id, email FROM users WHERE id = ?', 42);$db->execute(
'UPDATE users SET email = ? WHERE id = ?',
['[email protected]', 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');fetchAll by a column$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): selfexecute, fetch, and fetchAll method names remain unchanged.execute(..., $debug = true) is deprecated and should be replaced with setQueryLogger().encrypt => true) in production.Packagist picks up new versions from Git tags.
git tag -a v3.0.0 -m "Release v3.0.0"
git push origin v3.0.0Recommended 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.mdMIT
No open issues yet, or sync has not completed.