migliori/php-pdo-database-class
Composer 安装命令:
composer require migliori/php-pdo-database-class
包简介
The DB class is a database abstraction layer that provides a simple, consistent interface for interacting with different types of databases. It handles connection management, query execution, pagination and result processing
README 文档
README
⚠️ DEPRECATION NOTICE: This package is no longer maintained and has been deprecated. It's been replaced by PowerLite PDO. Please visit the PowerLite PDO GitHub repository for the latest updates and support.
This DB class provides a set of simple, intuitive methods for executing queries and retrieving data. It handles pagination, error handling and debugging.
The code is fully documented with PHPDOC. It provides types & type hinting and follows the highest coding standards (PHPSTAN Level 9).
Demo
PDO Database class - queries and pagination demos
Features
- Connection to any MySQL, Firebird, OCI (Oracle) or Pgsql (PostgreSQL) database
- SQL queries Sending
- Generation and sending of prepared PDO queries
- Functions available for all types of queries:
- Select
- SelectCount
- SelectRow
- SelectValue
- Query
- QueryRow
- QueryValue
- Execute
- Insert
- Update
- Delete
- GetColums
- GetColumnsNames
- GetTables
- TransactionBegin
- TransactionCommit
- TransactionRollback
- Pagination with configuration and options
- Register your connection settings in a single safe place
- DEBUG mode - display of SQL queries sent to the server and detailed information
- Error event handling and PHP error logging with try/catch
Requirements
PHP ^7.4, PHP 8.x
Documentation
PHP PDO Database class - Full detailed documentation, functions reference & code samples
Installation
Clone / download or install with Composer
composer require migliori/php-pdo-database-class
Usage/Examples
-
Open
src/connect/db-connect.phpin your code editor and set the followings constants to connect to your database:PDO_DRIVER // 'mysql', 'firebird', 'oci' or 'pgsql' DB_HOST // For instance 'localhost' DB_NAME // Your database name DB_USER // Your database username DB_PASS // Your database password DB_PORT[OPTIONAL] // The default port is 3306
-
Require
src/connect/db-connect.phpand you can connect to both your localhost and production server using$db = new DB();without any argument.use Migliori\Database\Db; // register the database connection settings require_once 'src/connect/db-connect.php'; // Then connect to the database $db = new DB(); // or connect and show all the encountered errors automatically $db = new DB(true); // or connect, then test the connection and retrieve the error if the database is not connected $db = new DB(); if (!$db->isConnected()) { $error_message = $db->error(); }
-
Call the methods to send your queries and retrieve the results.
// Select rows without using SQL $values = array('id', 'first_name', 'last_name'); $where = array('country' => 'Indonesia'); $db->select('customers', $values, $where); // We can make more complex where clauses in the Select, Update, and Delete methods $values = array('id', 'first_name', 'last_name'); $where = array( 'zip_code IS NOT NULL', 'id >' => 10, 'last_name LIKE' => '%Ge%' ); $db->select('customers', $values, $where); // Let's sort by descending ID and run it in debug mode $extras = array('order_by' => 'id DESC'); $db->select('customers', $values, $where, $extras, true); // loop through the results while ($row = $db->fetch()) { echo $row->first_name . ' ' . $row->last_name . '<br>'; } // or fetch all the records then loop // (this function should not be used if a huge number of rows have been selected, otherwise it will consume a lot of memory) $rows = $db->fetchAll(); foreach ($rows as $row) { echo $row->first_name . ' ' . $row->last_name . '<br>'; }
To see all the public methods and more examples of use of use visit https://www.phpformbuilder.pro/documentation/php-pdo-database-class.php
Example with Pagination
-
Open
database/db-connect.phpin your code editor and set the followings constants to connect to your database:PDO_DRIVER // 'mysql', 'firebird', 'oci' or 'pgsql' DB_HOST // For instance 'localhost' DB_NAME // Your database name DB_USER // Your database username DB_PASS // Your database password DB_PORT[OPTIONAL] // The default port is 3306
-
Get your records and the pagination HTML code
use Migliori\Database\Pagination; use Migliori\Database\PdoSelectParams; // register the database connection settings require_once 'src/connect/db-connect.php'; // register the PDO parameters for the query in a PdoSelectParams() object $values = array('id', 'first_name', 'last_name'); $where = array('country' => 'Indonesia'); $pdo_select_params = new PdoSelectParams('customers', $values, $where); // create the Pagination object $db = new Pagination($pdo_select_params); // get the records and the pagination HTML code $pagination_html = $db->pagine(); // count the records and display them $records_count = $db->rowCount(); if (!empty($records_count)) { while ($row = $db->fetch()) { echo $row->first_name . ' ' . $row->last_name . ' : ' . $row->country . '<br>'; } } echo $pagination_html;
Running Tests
To run tests, run the following command
php ./vendor/bin/phpunit test
Contributing
Contributions are always welcome!
Please contact us for any improvement suggestions or send your pull requests
License
migliori/php-pdo-database-class 适用场景与选型建议
migliori/php-pdo-database-class 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 435 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 migliori/php-pdo-database-class 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 migliori/php-pdo-database-class 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 435
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2023-11-06