定制 krubio/perfect-database 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

krubio/perfect-database

Composer 安装命令:

composer require krubio/perfect-database

包简介

SOLID Compliant Database Connection

README 文档

README

codecov Build

Coverage Maintainability Rating Code Smells Technical Debt Quality Gate Status Reliability Rating Duplicated Lines (%) Vulnerabilities Bugs Security Rating

Introduction:

These Database Connection Classes are designed to establish a connection with SQLite, MSSQL, Postgres, MongoDB and MySQL databases. These classes provide a common interface for connecting to different databases using PHP Data Objects (PDO) extension This end user documentation and usage instructions will help you to configure and use the SQLiteConnection and MysqlConnection classes in your PHP project.

Installation

You can install the package using Composer. Run the following command in your project directory:

composer require krubio/perfect-database

This will download the package and its dependencies and add the package to your composer.json file.

Usage

Once you have installed the package, you can use the SqliteConnection and MysqlConnection classes by importing them into your code and creating instances of them. Here is an example:

<?php

require_once 'vendor/autoload.php';

use PerfectApp\Database\SqliteConnection;
use PerfectApp\Database\MysqlConnection;

    // SQLite configuration
$config = [
    'path' => 'path/to/database/file',
    'options' => [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ],
];

// Create a SQLite connection
$sqliteConnection = new SqliteConnection();
$pdo = $sqliteConnection->connect($config);

// MySQL configuration
$config = [
    'host' => 'localhost',
    'port' => 3306,
    'dbname' => 'my_database',
    'charset' => 'utf8mb4',
    'username' => 'my_username',
    'password' => 'my_password',
    'options' => [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ],
];

// Create a MySQL connection
$mysqlConnection = new MysqlConnection();
$pdo = $mysqlConnection->connect($config);

Note that the vendor/autoload.php file is required to load the classes from the package using Composer's autoloading feature. If you're not using Composer, you'll need to manually include the files for the classes you want to use.

That's it! You can now use the PDO object returned by the connect method to interact with your database.

Example:

$stmt = $pdo->prepare("SELECT * FROM users WHERE id=:id");
$stmt->bindValue(':id', 1, PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);

Configuration:

To establish a connection with a database using these classes, you need to provide a configuration array with the required parameters. The configuration array must have the following keys:

For SqliteConnection:

  • 'path': The path to the SQLite database file.
  • 'options': An array of PDO options. (optional)

For MysqlConnection:

  • 'host': The MySQL server hostname or IP address.
  • 'port': The MySQL server port number.
  • 'dbname': The name of the MySQL database.
  • 'charset': The character set to use for the connection.
  • 'username': The MySQL username.
  • 'password': The MySQL password.
  • 'options': An array of PDO options. (optional)

Note: The configuration array must contain all the required keys. Otherwise, an InvalidArgumentException will be thrown.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固