migliori / php-pdo-database-class
DB类是一个数据库抽象层,为与不同类型的数据库交互提供了一个简单、一致的接口。它处理连接管理、查询执行、分页和结果处理。
v1.1
2024-03-08 06:55 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^10.4
README
⚠️ 弃用通知:此包不再维护且已被弃用。它已被PowerLite PDO替代。请访问PowerLite PDO GitHub仓库获取最新更新和支持。
此DB类提供了一组简单、直观的方法来执行查询和检索数据。它处理分页、错误处理和调试。
代码使用PHPDOC进行了全面文档记录。它提供了类型和类型提示,并遵循最高的编码标准(PHPSTAN Level 9)。
演示
特性
- 连接到任何MySQL、Firebird、OCI(Oracle)或Pgsql(PostgreSQL)数据库
- 发送SQL查询
- 生成和发送预处理的PDO查询
- 所有类型查询都可用
- 选择
- 选择计数
- 选择行
- 选择值
- 查询
- 查询行
- 查询值
- 执行
- 插入
- 更新
- 删除
- 获取列
- 获取列名
- 获取表
- 事务开始
- 事务提交
- 事务回滚
- 带有配置和选项的分页
- 在单一安全位置注册您的连接设置
- DEBUG模式 - 显示发送到服务器的SQL查询和详细信息
- 错误事件处理和PHP错误日志记录使用try/catch
要求
PHP ^7.4, PHP 8.x
文档
PHP PDO数据库类 - 完全详细文档,函数参考和代码示例
安装
克隆/下载或使用Composer安装
composer require migliori/php-pdo-database-class
使用/示例
-
在您的代码编辑器中打开
src/connect/db-connect.php并设置以下常量以连接到您的数据库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
-
要求
src/connect/db-connect.php,您可以使用$db = new DB();连接到您的本地主机和生产服务器,无需任何参数。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(); }
-
调用方法来发送您的查询并检索结果。
// 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>'; }
要查看所有公共方法和使用示例,请访问https://www.phpformbuilder.pro/documentation/php-pdo-database-class.php
分页示例
-
在您的代码编辑器中打开
database/db-connect.php并设置以下常量以连接到您的数据库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
-
获取您的记录和分页HTML代码
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;
运行测试
要运行测试,请运行以下命令
php ./vendor/bin/phpunit test
贡献
欢迎贡献!
请与我们联系以获取改进建议或发送您的pull请求