patrick-hull/mysql-wrapper

一个使用预处理语句的简单MySQL语句包装器

0.0.2 2024-10-02 04:01 UTC

This package is auto-updated.

Last update: 2024-10-02 04:01:53 UTC


README

MysqlWrapper是一个PHP库,旨在简化MySQL数据库操作。本软件包提供了一种简单而优雅的接口,用于执行常见的数据库查询,包括插入、更新、删除和选择操作。

目录

安装

注意:此软件仍被视为测试版。使用风险自负

您可以通过Composer安装此软件包。运行以下命令

composer require patrick-hull/mysql-wrapper

使用

DbConnection

DbConnection类负责建立与MySQL数据库的连接。

use PatrickHull\MysqlWrapper\DbConnection;

$dbConnection = new DbConnection('server_address', 'username', 'password');
$link = $dbConnection->connect();

DeleteQuery

DeleteQuery类允许您根据条件从指定的数据库表中删除记录。

use PatrickHull\MysqlWrapper\DeleteQuery;

$deleteQuery = new DeleteQuery($link);
$deleteQuery->database = 'your_database';
$deleteQuery->table = 'your_table';
$deleteQuery->criteria = ['column_name' => 'value'];

$result = $deleteQuery->Execute();

InsertQuery

InsertQuery类允许您将新记录插入到指定的数据库表中。

use PatrickHull\MysqlWrapper\InsertQuery;

$insertQuery = new InsertQuery($link);
$insertQuery->database = 'your_database';
$insertQuery->table = 'your_table';
$insertQuery->data = ['column1' => 'value1', 'column2' => 'value2'];

$result = $insertQuery->Execute();

UpdateQuery

UpdateQuery类用于根据指定的条件更新数据库表中的现有记录。

use PatrickHull\MysqlWrapper\UpdateQuery;

$updateQuery = new UpdateQuery($link);
$updateQuery->database = 'your_database';
$updateQuery->table = 'your_table';
$updateQuery->data = ['column1' => 'new_value'];
$updateQuery->criteria = ['column_name' => 'value'];

$result = $updateQuery->Execute();

SelectQuery

SelectQuery类允许您使用各种筛选选项从数据库表中检索数据。

use PatrickHull\MysqlWrapper\SelectQuery;

$selectQuery = new SelectQuery($link);
$selectQuery->database = 'your_database';
$selectQuery->table = 'your_table';
$selectQuery->criteria = ['column_name' => 'value'];
$selectQuery->limit = 10;

$result = $selectQuery->Execute();

在SelectQuery类中,您还可以利用Redis作为缓存解决方案。以下是一个示例

use PatrickHull\MysqlWrapper\SelectQuery;

$selectQuery = new SelectQuery($link);
$selectQuery->database = 'your_database';
$selectQuery->table = 'your_table';
$selectQuery->criteria = ['column_name' => 'value'];
$selectQuery->redis = true;
$selectQuery->redis_pw = "password";
$selectQuery->cache_duration = "200" //Number of Seconds the cache is live
$selectQuery->limit = 10;

$result = $selectQuery->Execute();

贡献

欢迎贡献!请提交一个拉取请求或提出一个问题来讨论潜在改进。

许可

本项目采用MIT许可证。有关详细信息,请参阅LICENSE文件。