victorvolpe/php-pdo-wrapper
最新稳定版本:v1.0.0
Composer 安装命令:
composer require victorvolpe/php-pdo-wrapper
包简介
A modern PDO wrapper for PHP 8.2+, supporting dynamic placeholders and secure database access.
README 文档
README
A simple, modern, and lightweight PHP PDO wrapper for MySQL (and compatible databases). Built with type safety, PSR standards, and inspired by lincanbin/PHP-PDO-MySQL-Class.
Features
- Easy database connections with automatic retry
- Secure query binding (including
IN (?)arrays support) - Fluent methods for common operations (select, insert, update, delete)
- Transaction support
- Error handling with automatic reconnection on server timeout
- Simple logging for connection and query errors
Installation
You can install via Composer:
composer require victorvolpe/php-pdo-wrapper
Or simply include DB.php manually if you prefer not to use Composer.
Usage
1. Instantiate the Database
use VictorVolpe\PhpPdoWrapper\DB; $db = new DB( dsn: 'mysql:host=localhost;dbname=testdb;charset=utf8mb4', user: 'your_username', pass: 'your_password' );
2. Basic Query
$users = $db->query("SELECT * FROM users WHERE status = :status", [ 'status' => 'active' ]);
3. Fetch a Single Row
$user = $db->row("SELECT * FROM users WHERE id = :id", [ 'id' => 1 ]);
4. Fetch a Single Column
$emails = $db->column("SELECT email FROM users WHERE status = :status", [ 'status' => 'active' ]);
5. Fetch a Single Value
$email = $db->single("SELECT email FROM users WHERE id = :id", [ 'id' => 1 ]);
6. Insert a New Record
$newUserId = $db->insert('users', [ 'username' => 'newuser', 'email' => 'newuser@example.com', 'status' => 'active' ]);
7. Update Existing Records
$rowsAffected = $db->update('users', [ 'status' => 'inactive' ], "last_login < :date", [ 'date' => '2024-01-01' ]);
8. Delete Records
$rowsDeleted = $db->delete('users', "status = :status", [ 'status' => 'inactive' ]);
9. Transactions
$db->begin(); try { $db->query("UPDATE accounts SET balance = balance - :amount WHERE id = :id", [ 'amount' => 100, 'id' => 1 ]); $db->query("UPDATE accounts SET balance = balance + :amount WHERE id = :id", [ 'amount' => 100, 'id' => 2 ]); $db->commit(); } catch (Exception $e) { $db->rollback(); throw $e; }
Logging
If a connection error or query exception occurs, a log file will automatically be created in:
/logs/db-YYYY-MM-DD.log
License
This project is open-sourced under the MIT License.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-27