https://github.com/bpolaszek/simple-dbal
Unified OOP API for PDO/Mysqli.
https://github.com/bpolaszek/simple-dbal
database mysql mysqli pdo php
Last synced: 8 months ago
JSON representation
Unified OOP API for PDO/Mysqli.
- Host: GitHub
- URL: https://github.com/bpolaszek/simple-dbal
- Owner: bpolaszek
- License: mit
- Created: 2017-05-18T13:23:29.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-10-23T09:54:40.000Z (over 5 years ago)
- Last Synced: 2024-04-02T01:21:07.922Z (about 2 years ago)
- Topics: database, mysql, mysqli, pdo, php
- Language: PHP
- Homepage:
- Size: 62.5 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/bentools/simple-dbal)
[](https://packagist.org/packages/bentools/simple-dbal)
[](https://scrutinizer-ci.com/g/bpolaszek/simple-dbal)
[](https://packagist.org/packages/bentools/simple-dbal)
# SimpleDBAL
A modern wrapper on top of PDO and Mysqli, written in PHP7.1. It aims at exposing the same API regardless of the one your project uses.
Introduction
------------
PHP offers 2 different APIs to connect to a SQL database: `PDO` and `mysqli` (we won't talk about `mysql_*` functions since they're deprecated). Both do more or less the same thing, but:
* Their API are completely different (different method names and signatures)
* They both have their own pros and cons.
* Some features of one are missing in the other.
This library exposes an API that can be used by any of them, transparently, in a more modern approach (OOP, iterators, return types, etc).
This also means you can switch from `PDO` to `mysqli` and vice-versa without having to rewrite your whole code.
From my personnal experience, I was used to `PDO` and was forced to deal with `mysqli` in another project and it was really messy.
Overview
--------
```php
use BenTools\SimpleDBAL\Model\Credentials;
use BenTools\SimpleDBAL\Model\SimpleDBAL;
$credentials = new Credentials('localhost', 'user', 'password', 'database');
$cnx = SimpleDBAL::factory($credentials, SimpleDBAL::PDO);
$query = "SELECT `id`, `name` FROM guys WHERE created_at > ?";
foreach ($cnx->execute($query, [new DateTime('-1 month')]) as $item) {
var_dump($item['name']);
}
```
Additionnal features
--------------------
* On-the-fly parameter binding for prepared statements (simply pass an array of arguments after the query)
* `DateTimeInterface` objects automatic binding (formats to YYYY-MM-DD HH:ii:ss)
* [Asynchronous queries](doc/03-AsynchronousQueries.md) (Promises)
* [Parallel queries](doc/03-AsynchronousQueries.md#parallel-queries)
* Support for named parameters in prepared statements for `mysqli` (polyfill with regexps - experimental feature)
* Can [silently reconnect](doc/02-Configuration.md) after a lost connection
The `Result` object
------------------
Every query sent to the adapter will return a `BenTools\SimpleDBAL\Contract\ResultInterface` object.
For _SELECT_ queries, use the following methods:
* `$result->asArray()` to fetch an array containing the whole resultset
* `$result->asRow()` to fetch the 1st row of the resultset, as an associative array
* `$result->asList()` to fetch the 1st column of the resultset, as a sequential array
* `$result->asValue()` to fetch a single value (i.e the 1st column of the 1st row)
* `foreach ($result as $row)` to iterate over the resultset (uses lazy-loading)
* `count($result)` to return the number of rows of the resultset.
For _INSERT_ / _UPDATE_ / _DELETE_ queries, use the following methods:
* `count($result)` to return the number of affected rows
* `$result->getLastInsertId()` to get the id of the last inserted row or sequence value.
Installation
------------
```
composer require bentools/simple-dbal
```
Tests
-----
```
./vendor/bin/phpunit
```
Documentation
-----
[Getting started](doc/01-GettingStarted.md)
[Configuring auto-reconnect](doc/02-Configuration.md)
[Asynchronous and parallel queries](doc/03-AsynchronousQueries.md)
[Connection pools](doc/04-ConnectionPools.md)
[Known Issues](doc/05-KnownIssues.md)