power lite pdo
v1.1.1
PowerLite PDO 是一个轻量级、功能强大的 PHP 库,它提供了一种使用 PHP 数据对象 (PDO) 与数据库交互的简单而有效的方法。它支持多个数据库驱动程序,并包括轻松连接管理、查询执行、结果处理和分页等功能。
PHP ^7.4、PHP 8.x
PowerLite PDO 的文档可在 PowerLite PDO 网站上找到。
除了文档之外,还可以在此处获取 PHPDoc,以获取有关类、方法及其参数的更多详细信息。
使用 Composer 克隆/下载或安装
composer require migliori/power-lite-pdo
在代码编辑器中打开src/connection.php
并将常量的值替换为数据库连接设置(DB_HOST、DB_NAME、DB_USER、DB_PASS、DB_PORT、DB_CHARSET)。
为了增强安全性,请将文件存储在 Web 服务器的文档根目录(提供给 Internet 的目录)之外,并在配置文件 ( src/config.php
) 中相应地更改路径。这会阻止通过 URL 直接访问该文件。
包含引导文件并从容器中获取 Db 实例:
use Migliori PowerLitePdo Db ;
// 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);
使用 Db 类中的 select 方法选择一些记录:
$ 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 );
逐条获取选中的记录:
while ( $ record = $ db -> fetch ()) {
echo $ record -> id . ' , ' . $ record -> username . ' , ' . $ record -> email . "n" ;
}
包含引导文件并从容器中获取 QueryBuilder 实例:
use Migliori PowerLitePdo Query QueryBuilder ;
// 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);
使用 QueryBuilder 选择一些记录:
$ queryBuilder -> select ([ ' id ' , ' username ' , ' email ' ])-> from ( ' users ' )-> where ([ ' status ' => ' active ' ])-> execute ();
逐条获取选中的记录:
while ( $ record = $ queryBuilder -> fetch ()) {
echo $ record -> id . ' , ' . $ record -> username . ' , ' . $ record -> email . "n" ;
}
包含引导文件并从容器中获取 Pagination 实例:
use Migliori PowerLitePdo Pagination ;
// 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);
选择一些记录:
$ 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 );
一条一条地获取选中的记录:
while ( $ record = $ pagination -> fetch ()) {
echo $ record -> id . ' , ' . $ record -> username . ' , ' . $ record -> email . "n" ;
}
显示分页:
$ url = ' /users ' ; // The URL for the pagination links
echo $ pagination -> pagine ( $ url );
要运行测试,请运行以下命令
php ./vendor/bin/phpunit test
随时欢迎您的贡献!
请联系我们获取任何改进建议或发送您的拉取请求
GNU 通用公共许可证 v3.0