2017 © Pedro Peláez
 

library php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

image

docnet/php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  • Monday, January 29, 2018
  • by Docnet
  • Repository
  • 14 Watchers
  • 1 Stars
  • 1,347 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 4 Open issues
  • 10 Versions
  • 12 % Grown

The README.md

PHP DB Access Layer

Designed to wrap mysqli into a series of fast, secure and easy to use classes., (*1)

Install with Composer

Require line as follows, (*2)

"docnet/php-dbal": "v2.1", (*3)

Let's Connect

You're gonna need to connect to a DB before you can do anything else..., (*4)

<?php
$settings = new \Docnet\DB\ConnectionSettings('127.0.0.1', 'root', 'password', 'dbname');
$db = new \Docnet\DB($settings);

For the following examples, we'll assume there's an active DB object., (*5)

My First SELECT

After this has executed, $records will be an array of stdClass objects - see how to change the result class later., (*6)

<?php
$records = $db->fetchAll("SELECT * FROM tblData");

SELECT One Record

After this has executed, $record will be a stdClass object., (*7)

<?php
$record = $db->fetchOne("SELECT * FROM tblData WHERE intKey = ?", 84);

Result Class

If we pass in an optional third parameter, we'll get back an object of that class, (*8)

<?php
$foo = $db->fetchOne("SELECT * FROM tblData WHERE intKey = ?", 84, 'Foo');

So now, $foo is an instance of class Foo, (*9)

SELECT with parameters and result Class

After execution, $records is an array of (namespaced) \Docnet\Bar objects, where intKey > 3 and vchName = Barry, (*10)

<?php
$records = $db->prepare("SELECT * FROM tblData WHERE intKey > ?id AND vchName = ?name")
   ->bindInt('id', 3)
   ->bindString('name', 'Barry')
   ->setResultClass('\\Docnet\\Bar')
   ->fetchAll();

The prepare() method returns a fluent Statement class which provides named parameter binding., (*11)

Parameter binding deals with all escaping and quoting for you., (*12)

INSERT, UPDATE, DELETE

Insert, update and delete operations (also called DML queries) work in just the same way as the fetch methods., (*13)

<?php
$binds = array(1, 'foo');
$db->insert("INSERT INTO tblData (intField1, vchField2) VALUES (?, ?)", $binds);

Return Values

  • insert() returns the last insert id
  • update() and delete() return the number of affected rows

Re-executing Prepared Statements

For SELECTs, (*14)

<?php
$stmt = $db->prepare("SELECT * FROM tblData WHERE intKey = ?id");
$stmt->bindInt('id', 4)->fetchOne();
$stmt->bindInt('id', 5)->fetchOne();

Or, more commonly, INSERTs - this can be MUCH higher performance than running multiple INSERT queries as the server only interprets the SQL string once., (*15)

<?php
$stmt = $db->prepare("INSERT INTO tblPeople VALUES (?name");
$stmt->bindString('name', 'Tom')->insert();
$stmt->bindString('name', 'Dick')->insert();
$stmt->bindString('name', 'Harry')->insert();

Arbitrary SQL

If you REALLY need to, you can just run arbitrary queries like this:, (*16)

<?php
$db->query("TRUNCATE tblTransient");

Binding

Binding is great. It allows the DBAL to take care of escaping AND quoting., (*17)

There are quite a few different supported binding methods (probably too many, but I'm keen to be flexible)., (*18)

Shorthand, single scalar value, (*19)

<?php
$db->fetchOne("SELECT * FROM tblData WHERE intKey = ?", 84);

Shorthand array of parameters - parameter sequence must match your query, (*20)

<?php
$db->fetchOne("SELECT * FROM tblData WHERE intKey = ? AND vchName = ?", array(84, 'Tom'));

Shorthand array of named parameters - any sequence, types auto-detected, (*21)

<?php
$params = array('name' => 'Tom', 'id' => 84);
$db->fetchOne("SELECT * FROM tblData WHERE intKey = ?id AND vchName = ?name", $params);

Long-hand typed, named binding - fluent, any sequence, (*22)

<?php
$db->prepare("SELECT * FROM tblData WHERE intKey = ?id AND vchName = ?name")
   ->bindString('name', 'Dick')
   ->bindInt('id', 4)
   ->fetchOne();

Long-hand type-hinted, named binding - fluent, any sequence, (*23)

<?php
$db->prepare("SELECT * FROM tblData WHERE intKey = ?int_id AND vchName = ?str_name")
   ->bind('str_name', 'Dick')
   ->bind('int_id', 4)
   ->fetchOne();

Public Methods

DB Class

The following methods are available, (*24)

  • fetchOne()
  • fetchAll()
  • insert()
  • update()
  • delete()
  • prepare() which returns a new Statement object when called
  • query()
  • escape()
  • begin() Transaction support
  • commit() Transaction support
  • rollback() Transaction support

Statement Class

SELECT - fetchOne() - fetchAll() - setResultClass(), (*25)

DML - insert() - update() - delete(), (*26)

Binding - bind() - bindInt() - bindString() - bindDouble() - bindBlob(), (*27)

Post execution - getInsertId() - getAffectedRows(), (*28)

The Versions

29/01 2018

dev-master

9999999-dev https://github.com/DocnetUK/php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Avatar Docnet

mysqli docnet

29/01 2018

v2.2.1

2.2.1.0 https://github.com/DocnetUK/php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Avatar Docnet

mysqli docnet

02/02 2016

v2.2

2.2.0.0 https://github.com/DocnetUK/php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Avatar Docnet

mysqli docnet

27/10 2015

v2.1

2.1.0.0 https://github.com/DocnetUK/php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Avatar Docnet

mysqli docnet

19/06 2015

v2.0

2.0.0.0 https://github.com/DocnetUK/php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Avatar Docnet

mysqli docnet

30/04 2015

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/DocnetUK/php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Avatar Docnet

mysqli docnet

30/04 2015

v1.1.1

1.1.1.0 https://github.com/DocnetUK/php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Avatar Docnet

mysqli docnet

31/07 2014

v1.1.0

1.1.0.0 https://github.com/DocnetUK/php-dbal

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Avatar Docnet

mysqli docnet

03/02 2014

v1.0.1

1.0.1.0

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Avatar Docnet

01/02 2014

v1.0.0

1.0.0.0

A library which wraps mysqli into a fluent, easy-to-use set of classes.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Avatar Docnet