Optimized sql libary for php! (currently only for MySQL)
With that libary, you can save a lot of time, lines of code and spare nerves! It provides easy and compact methods for faster working.
- Higher code clarity
- Easier to store connection data in an other file
- Use DatabaseManager::class to store database connections, instead of global variables or extra methods for accessing the database
- bindParam() automatically treats numeric values as integers (no intval() and PDO::PARAM_INT required)
- PHP 7.0 or newer
- PHP extensions pdo and pdo-mysql
Vanilla-PDO:
$mainDatabase = new PDO("mysql:host=localhost;dbname=foo;charset=utf8", 'root', 'bar');
Better-PDO:
DatabaseManager::registerConnection('main', new BetterPDO(array(
'host' => 'localhost',
'database' => 'foo',
'username' => 'root',
'password' => 'bar',
'charset' => 'utf8'
)));
Vanilla-PDO:
function getUser($userId) {
global $mainDatabase;
$sql = 'SELECT * FROM `user` WHERE `user_id` < :userId';
$stmt = $mainDatabase->prepare($sql);
$stmt->bindParam(':userId', $userId);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
Better-PDO:
function getUser($userId) {
$sql = 'SELECT * FROM `user` WHERE `user_id` < :userId';
return DatabaseManager::getConnection('main')
->createStatement($sql)
->bindParam(':userId', $userId)
->fetch(FetchStyle::ASSOC);
}
composer config maaxgr.betterpdo vcs https://github.com/MaaxGr/BetterPDO
composer require maaxgr.betterpdo:dev-master
composer update
- Clone project or download all files of src/ directory
- require_once('/autoload.php');