onebytesolutions/database 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

onebytesolutions/database

Composer 安装命令:

composer require onebytesolutions/database

包简介

Database Library - A dependency injection based Database Class

README 文档

README

Database Library - A dependency injection based Database Class

### Table of Contents

Installation
Initialization
Insert Query
Insert Batch Query
Update Query
Select Query
Delete Query
Raw Query

About

This software was developed during my free time and is free to use.

Installation

Composer

composer require onebytesolutions/database

Initialization

Simple initialization:

use OneByteSolutions\Database\Database,
    OneByteSolutions\Database\Adapters\PdoAdapter;

// set db config
$db = [
    'host' => 'localhost',
    'user' => 'db-username',
    'pass' => 'db-password',
    'database' => 'database-name'
	// OPTIONAL: ,'port' => 3308 
];

// try to connect to the database
try {
    $database = new Database(new PdoAdapter($db));
    $database->connect();
}catch (\Exception $e){
    echo "Unable to connect: ".$e->getMessage();
}

Insert Query

Simple example

// example of inserting a new user, with transactions
$database->beginTransaction();
try {
    $row = [
        'name' => 'John Doe',
        'email' => 'john.doe@example.org'
    ];
    $id = $database->insertRow("users", $row);
    $database->commit();
    
    echo 'inserted id: '.$id;
} catch(\Exception $e){
    echo 'insert failure';
    $database->rollBack();
}

Insert Batch Query

Simple example

// example of inserting a bunch of rows
$rows = [];
$rows[] = [
        'name' => 'John Doe ',
        'email' => 'john.doe@example.org'
];
$rows[] = [
        'name' => 'Jane Doe ',
        'email' => 'jane.doe@example.org'
];
$rows[] = [
        'name' => 'Sarah Doe ',
        'email' => 'sarah.doe@example.org'
];
$database->insertRowBatch("users", $rows);

Update Query

// example of updating a row
$database->updateRowWhere("user", ["lastLogin" => time()], "id", 1);

Select Query

// example of fetching a query as an array
$sql = "SELECT * FROM users LIMIT 30";
$params = [];
$results = $database->queryToArray($sql, $params);

echo '<pre>'.print_r($results,true).'</pre>';

Delete Query

// example of deleting a row
$database->deleteWhere("users", "id", 1);

Raw Query

// example of running raw sql
$database->run("UPDATE users SET name = :name", ['name' => 'Alex Doe']);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2019-06-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固