PowerLite PDO is a lightweight, powerful PHP library that provides a simple and efficient way to interact with databases using PHP Data Objects (PDO). It supports multiple database drivers and includes features like easy connection management, query execution, result handling and pagination.
PHP ^7.4, PHP 8.x
The documentation for PowerLite PDO is available on the PowerLite PDO website.
In addition to the documentation, a PHPDoc is also available here for more detailed information about the classes, methods, and their parameters.
Clone / download or install with Composer
composer require migliori/power-lite-pdo
Open src/connection.php
in your code editor and replace the constant's values with your database connection settings (DB_HOST, DB_NAME, DB_USER, DB_PASS, DB_PORT, DB_CHARSET).
For enhanced safety, store the file outside of your web server's document root (the directory that is served to the internet) and change the path accordingly in the configuration file (src/config.php
). This prevents the file from being directly accessible via a URL.
Include the bootstrap file and get the Db instance from the container:
use MiglioriPowerLitePdoDb;
// Build the container and connect to the database
$container = require_once __DIR__ . '/vendor/migliori/power-lite-pdo/src/bootstrap.php';
$db = $container->get(Db::class);
Use the select method from the Db class to select some records:
$from = 'users'; // The table name
$fields = ['id', 'username', 'email']; // The columns you want to select
$where = ['status' => 'active']; // The conditions for the WHERE clause
$db->select($from, $fields, $where);
Fetch the selected records one by one:
while ($record = $db->fetch()) {
echo $record->id . ', ' . $record->username . ', ' . $record->email . "n";
}
Include the bootstrap file and get the QueryBuilder instance from the container:
use MiglioriPowerLitePdoQueryQueryBuilder;
// Build the container and connect to the database
$container = require_once __DIR__ . '/vendor/migliori/power-lite-pdo/src/bootstrap.php';
$queryBuilder = $container->get(QueryBuilder::class);
Use the QueryBuilder to select some records:
$queryBuilder->select(['id', 'username', 'email'])->from('users')->where(['status' => 'active'])->execute();
Fetch the selected records one by one:
while ($record = $queryBuilder->fetch()) {
echo $record->id . ', ' . $record->username . ', ' . $record->email . "n";
}
Include the bootstrap file and get the Pagination instance from the container:
use MiglioriPowerLitePdoPagination;
// Build the container and connect to the database
$container = require_once __DIR__ . '/vendor/migliori/power-lite-pdo/src/bootstrap.php';
$pagination = $container->get(Pagination::class);
Select some records:
$from = 'users'; // The table name
$fields = ['id', 'username', 'email']; // The columns you want to select
$where = ['status' => 'active']; // The conditions for the WHERE clause
$pagination->select($from, $fields, $where);
Fetch the selected records one by one:
while ($record = $pagination->fetch()) {
echo $record->id . ', ' . $record->username . ', ' . $record->email . "n";
}
Display the pagination:
$url = '/users'; // The URL for the pagination links
echo $pagination->pagine($url);
To run tests, run the following command
php ./vendor/bin/phpunit test
Contributions are always welcome!
Please contact us for any improvement suggestions or send your pull requests
GNU General Public License v3.0