定制 hengeb/db 二次开发

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

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

hengeb/db

最新稳定版本:v0.5

Composer 安装命令:

composer require hengeb/db

包简介

A simple mysql library (PDO wrapper)

README 文档

README

A simple MySQL library for PHP (PDO wrapper)

Installation

Install the latest version with

$ composer require hengeb/db

Basic Usage

<?php

use Hengeb\Db\Db;

$db = new Db([
    "host" => "example.org",
    "port" => 3306,
    "database" => "my_database",
    "user" => "johndoe",
    "password" => "secret"
]);

// alternative:
$db = Db::getInstance();
/**
 * in this case the configuration will be loaded from environment variables
 * - MYSQL_HOST (default: localhost)
 * - MYSQL_PORT (default: 3306)
 * - MYSQL_DATABASE (default: database)
 * - MYSQL_USER (no default, required)
 * - MYSQL_PASSWORD (no default, required)
 */

// query() and execute() return insert id if a row was inserted
$id = $db->query("INSERT INTO contacts SET name=:name, phone=:phone", [
    "name" => "Jane",
    "phone" => "555-123",
])->getInsertId();
// e.g. $id === 4

// get single value
$phone = $db->query("SELECT phone FROM contacts WHERE id=:id", ["id" => $id])->get();
// $phone === "555-123"

// get single key for multiple rows
$allNames = $db->query("SELECT name FROM contacts ORDER BY name")->getColumn();
// $allNames === ["Alice", "Bob", "Jane", "Joe"]

// get single row
$contact = $db->query("SELECT name, phone FROM contacts WHERE id=:id", ["id" => $id])->getRow();
// $contact === ["name" => "Jane", "phone" => "555-123"]

// get associative array with the datra
$contacts = $db->query("SELECT name, phone FROM contacts ORDER BY name")->getAll();
// $contacts === [["name" => "Alice", "phone" => "555-987"], ...]

// reuse prepared statement
$names = [];
$statement = $db->prepare("SELECT name FROM contacts WHERE id=:id");
for ([1, 2, 3, 4] as $id) {
    $names[] = $statement->bind(["id" => $id])->execute()->get();
}
// $names === ["Bob", "Alice", "Joe", "Jane"]

// transaction:
$db->beginTransaction();
$db->query("INSERT INTO contacts SET name=:name, phone=:phone", [
    "name" => "Claire",
    "phone" => "555-222",
]);
$db->commit(); // or $db->rollback()

Author

Henrik Gebauer - code@henrik-gebauer.de - https://www.henrik-gebauer.de

License

This software is licensed under the MIT License - see the LICENSE file for details

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-16