spaniakos / simple-php-db-wrapper
简单的PDO数据库包装器
dev-main
2023-11-01 20:29 UTC
Requires
- monolog/monolog: ^3.4
- symfony/dotenv: ^6.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.35
- phpunit/phpunit: ^10.4.1
This package is auto-updated.
Last update: 2024-09-30 01:43:45 UTC
README
这是一个非常简单的PHP和MySQL数据库包装器,让您快速开始。简单轻盈,适合快速项目。
您可以通过以下方式导入:
composer require spaniakos/simple-php-db-wrapper
不要忘记向您的项目添加一个 .env 文件,或者将以下内容添加到您的 .env 文件中:
DEBUG=false
DB_HOST="localhost"
DB_USER="root"
DB_PASS="toor"
DB_NAME="test_database"
DB_PORT=3306
DB_CHARSET="utf8mb4"
DB_COLLATION="utf8mb4_general_ci"
LOG_PATH="path of log file with proper permissions"
以下是测试的配置信息
DB_CHARSET="utf8mb4"
DB_COLLATION="utf8mb4_general_ci"
请将上述内容修改为您所需的值
然后在您的脚本中,如果您还没有在其他地方做过,需要引入vendor autoload:require_once 'vendor/autoload.php';
然后您可以通过上述行使用库:
use Spaniakos\SimplePhpDbWrapper;
类中的示例用法
<?php declare(strict_types=1);
require_once 'vendor/autoload.php';
use Spaniakos\SimplePhpDbWrapper;
class MyClass {
protected $db;
/**
* Set up the test environment before each test.
*/
public function __construct() {
// Create an instance of the Database class before each test
$this->db = new SimplePhpDbWrapper(true);
}
public function testGetColFromTable() {
$result = $this->db->GetColFromTable('users', 'username', 'id > 0', 'username', '5');
}
}
需求
php >= 7.4
composer
它使用monolog记录PDO sql的错误消息
依赖项
monolog/monolog: ^3.4
symfony/dotenv: ^6.3
开发依赖项
friendsofphp/php-cs-fixer: ^3.35
phpunit/phpunit: ^10.4.1
安装
composer install
使用方法
更改 .env 中的凭据。您可以将 env.sample 复制到 .env,然后从PHP文件中引入类。
DB_HOST=your_database_host
DB_USER=your_database_user
DB_PASS=your_database_password
DB_NAME=your_database_name
DB_PORT=3306
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_0900_ai_ci
LOG_PATH=path/to/your/logfile.log
如何在文件中包含它
require_once('database.class.php');
$db = new Database();
如何调用函数(示例)
原始查询
$query = 'select * from table;
$rs = $db->Query($query);
更新示例:UpdateTable($table,$set,$where= ''){
$db->UpdateTable("table","column='data',column2=1","column3='where' and column4=2");
从表中获取所有数据:GetAllFromTable($table,$where = '',$order_col = 'ID',$order = 'ASC', $limit = '') {
$rs = $db->GetAllFromTable("table","username='user1' and password='password'",'id','asc', 1000) {
遍历结果
foreach($rs as $data){
//do
}
未来工作
* Change to Depencancy injection from env
* Change unit tests from mysql to mysql-lite for automated testing
* Write better examples
* Stracture the function to use arrays in instead of plain string in order to have better and simpler understanding of the Injecttion methods
* Consider Chainable methods