定制 haukurh/dbal 二次开发

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

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

haukurh/dbal

Composer 安装命令:

composer require haukurh/dbal

包简介

A simple and lightweight database abstraction layer

README 文档

README

A simple and lightweight database abstraction layer, which offers a API for basic CRUD operations but can be extended at will to fit every scenario.

Installation

The easiest way to implement haukurh/dbal in your project is to require it with composer.

composer require haukurh/dbal

Initialize

Initialize the database connection.

<?php

require_once 'vendor/autoload.php';

use Haukurh\DBAL\DB;
use Haukurh\DBAL\DSN\DSN;

$db = new DB(
    DSN::mysql(
        'example_db',
        'localhost',
        3306,
        'UTF8'
    ),
    'username',
    'password',
);

Basic usage

Like previously mentioned, the DB class offers some of the most basic CURD operations which are easy to read and write.

<?php

$contents = [
    'title' => 'Lorem ipsum dolor sit',
    'content' => 'Pellentesque rhoncus dui vitae tincidunt pulvinar...'
];

// Insert a record to the database
$db->insert('articles', $contents);

// Fetch all articles in descending order
$articles = $db->fetchAll('articles', 'ORDER BY id DESC');

// Fetch the next row from a result set
$article = $db->fetch('articles');

// Update records
$db->update('articles', [
    'title' => 'Updated title',
], 'WHERE id = :id', [':id' => 3]);

Named parameters

Dynamic parameters of an SQL query can be dangerous if not implemented correctly, the DB class has built in support for named parameters in prepared statements. Which can be circumvented but that would not be very smart.

Example on using named parameters:

<?php

$articles = $db->fetchAll('articles', 'WHERE id >= :id and title like :term', [
    ':id' => 10,
    ':term' => '%ipsum%',
]);

Example on how NOT to implement:

<?php

$id = 10;
$term = '%ipsum%';

$articles = $db->fetchAll('articles', "WHERE id >= {$id} and title like {$term}");

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固