GithubHelp home page GithubHelp logo

dbal's Introduction

haukurh/dbal

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}");

dbal's People

Contributors

haukurh avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.