承接 ideaglory/database 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ideaglory/database

最新稳定版本:v1.0.0

Composer 安装命令:

composer require ideaglory/database

包简介

PHP Singleton Database Class

README 文档

README

A lightweight and reusable PHP class to manage database connections using the Singleton pattern. This class ensures only one database connection instance exists during the runtime, providing an efficient and easy-to-use database interface.

Features

  • Singleton pattern to prevent multiple connections.
  • Parameterized query support to prevent SQL injection.
  • Basic transaction management (beginTransaction, commit, rollback).
  • Utility methods for fetching data (fetchAll, fetchOne).
  • Supports prepared statements with bound parameters.
  • Auto-close connection and reset instance when closed.

Installation

You can install this package via Composer. Run the following command:

composer require ideaglory/database

Usage

Basic Initialization

The Database class uses the Singleton pattern. To initialize it:

use Ideaglory\Database;

$db = Database::getInstance('host', 'username', 'password', 'database', 'charset');

Querying the Database

Execute a Query

To execute a query with parameters:

$sql = "INSERT INTO users (name, email) VALUES (?, ?)";
$params = ['John Doe', 'john.doe@example.com'];
$db->query($sql, $params);

Fetch All Rows

$sql = "SELECT * FROM users WHERE status = ?";
$params = ['active'];
$users = $db->fetchAll($sql, $params);
print_r($users);

Fetch a Single Row

$sql = "SELECT * FROM users WHERE id = ?";
$params = [1];
$user = $db->fetchOne($sql, $params);
print_r($user);

Transactions

Begin a Transaction

$db->beginTransaction();

Commit a Transaction

$db->commit();

Rollback a Transaction

$db->rollback();

Close Connection

When the database connection is no longer needed:

$db->close();

Use Cases

  1. Efficient Database Management: Ensures only one instance of the database connection exists during runtime, improving resource utilization.
  2. Simplified Query Execution: Provides easy-to-use methods for executing queries and fetching results.
  3. Secure Applications: Prevents SQL injection with parameterized queries.
  4. Transaction Support: Handles database transactions for complex operations.

Example

Below is a complete example demonstrating various features:

use Ideaglory\Database;

try {
    $db = Database::getInstance('localhost', 'root', '', 'example_db', 'utf8mb4');

    // Insert a new user
    $db->query("INSERT INTO users (name, email) VALUES (?, ?)", ['Alice', 'alice@example.com']);

    // Fetch all active users
    $users = $db->fetchAll("SELECT * FROM users WHERE status = ?", ['active']);
    print_r($users);

    // Start a transaction
    $db->beginTransaction();

    // Update a user
    $db->query("UPDATE users SET email = ? WHERE id = ?", ['alice.new@example.com', 1]);

    // Commit the transaction
    $db->commit();

} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
    $db->rollback(); // Rollback if an error occurs
} finally {
    $db->close(); // Close the connection
}

Contributing

Feel free to fork this repository and submit pull requests for improvements or bug fixes.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Created by IdeaGlory.

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-23