A modern PHP database wrapper for MySQL with prepared statements, connection pooling, and proper error handling.
- UTF-8/UTF8MB4 Connection Support
- Prepared Statements by Default
- Transaction Support
- Connection Pooling
- Automatic Retry Mechanism
- Proper Exception Handling
- Type Safety
- PSR-4 Autoloading
- Modern PHP 7.4+ Features
- PHP 7.4 or higher
- MySQL 5.7+ or MariaDB 10.3+
- PHP MySQLi extension
composer require shivanraptor/php-db-manager
- Download the latest release
- Include the autoloader in your project:
require_once('vendor/autoload.php');
use PhpDbManager\DbManager;
try {
$db = new DbManager([
'host' => 'localhost',
'username' => 'root',
'password' => 'your_password',
'database' => 'your_database',
'charset' => 'utf8mb4',
'port' => 3306,
'persistent' => false,
'autocommit' => true,
'retry_attempts' => 3,
'retry_delay' => 100 // milliseconds
]);
// Execute a prepared statement
$result = $db->execute(
"SELECT * FROM users WHERE id = ? AND status = ?",
['i' => 1, 's' => 'active']
);
// Fetch a single row
$user = $db->fetch($result);
// Or fetch all rows
$users = $db->fetchAll($result);
} catch (Exception $e) {
error_log('Database error: ' . $e->getMessage());
// Handle error appropriately
}
Option | Type | Default | Description |
---|---|---|---|
host | string | 'localhost' | Database host |
username | string | - | Database username |
password | string | - | Database password |
database | string | - | Database name |
charset | string | 'utf8mb4' | Connection charset |
port | int | 3306 | Database port |
persistent | bool | false | Use persistent connection |
autocommit | bool | true | Enable autocommit |
timeout | int | 30 | Connection timeout in seconds |
retry_attempts | int | 3 | Number of connection retry attempts |
retry_delay | int | 100 | Delay between retries in milliseconds |
// Select query
$result = $db->execute("SELECT * FROM users WHERE id = ?", ['i' => 1]);
$user = $db->fetch($result);
// Insert query
$db->execute(
"INSERT INTO users (name, email) VALUES (?, ?)",
['s' => 'John Doe', 's' => '[email protected]']
);
$userId = $db->lastInsertId();
// Update query
$db->execute(
"UPDATE users SET status = ? WHERE id = ?",
['s' => 'active', 'i' => 1]
);
$affectedRows = $db->affectedRows();
try {
$db->beginTransaction();
$db->execute(
"INSERT INTO orders (user_id, total) VALUES (?, ?)",
['i' => 1, 'd' => 99.99]
);
$db->execute(
"UPDATE inventory SET stock = stock - 1 WHERE product_id = ?",
['i' => 123]
);
$db->commit();
} catch (Exception $e) {
$db->rollback();
throw $e;
}
// Fetch as associative array
$result = $db->execute("SELECT * FROM users");
$users = $db->fetchAll($result, 'assoc');
// Fetch as object
$result = $db->execute("SELECT * FROM users");
$users = $db->fetchAll($result, 'object');
// Fetch as indexed array
$result = $db->execute("SELECT * FROM users");
$users = $db->fetchAll($result, 'array');
$info = $db->getConnectionInfo();
echo "Connected to {$info['server']} as {$info['user']}";
echo "MySQL version: {$info['version']}";
echo "Charset: {$info['charset']}";
- Constructor Changes:
// Old version
$db = new dbManager($host, $user, $pass, $dbname);
// New version
$db = new DbManager([
'host' => $host,
'username' => $user,
'password' => $pass,
'database' => $dbname
]);
- Namespace Required:
use PhpDbManager\DbManager;
- Error Handling:
// Old version
if ($db->error !== NULL) {
// error exists
}
// New version
try {
$db = new DbManager($options);
} catch (Exception $e) {
// Handle error
}
- Query Execution:
// Old version
$result = $db->query_prepare($sql, $params);
$row = $db->result($result);
// New version
$result = $db->execute($sql, $params);
$row = $db->fetch($result);
- Fork the repository
- Create your feature branch
- Run tests and code style checks
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support, please open an issue in the GitHub repository or contact the maintainers.