PHPackages                             simplon/db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. simplon/db

ActiveLibrary[Database &amp; ORM](/categories/database)

simplon/db
==========

Simplon DB Libraries

1.2.10(12y ago)22901MITPHPPHP &gt;=5.4

Since Oct 21Pushed 12y agoCompare

[ Source](https://github.com/fightbulc/simplon_db)[ Packagist](https://packagist.org/packages/simplon/db)[ Docs](https://github.com/fightbulc/simplon_db)[ RSS](/packages/simplon-db/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (19)Used By (0)

```
     _                 _                   _ _
 ___(_)_ __ ___  _ __ | | ___  _ __     __| | |__
/ __| | '_ ` _ \| '_ \| |/ _ \| '_ \   / _` | '_ \
\__ \ | | | | | | |_) | | (_) | | | | | (_| | |_) |
|___/_|_| |_| |_| .__/|_|\___/|_| |_|  \__,_|_.__/
                |_|
```

Simplon/Db
==========

[](#simplondb)

Version 1.1.0

Intro
-----

[](#intro)

Most of my projects require data from at least one type of database. In order to handle all communications I wrote some interface libraries which help me to deal with my daily coding fun. I worked with all supported databases hence all these interfaces were written.

For the last months I am running mostly with a MySQL setup supported with Redis as a cache store. I worked with Memcached before which I believe is stable on what its supposed to do. I closed the Couchbase chapter for now although I believe its a great idea. However, I wasnt really happy with its performance respectively its immature behaviour compared to e.g. Redis.

### Supported databases

[](#supported-databases)

- [MySQL](http://mysql.com/) &gt;= 5.1
- [Redis](http://redis.io/) &gt;= 2.4
- [Couchbase](http://www.couchbase.com/) &gt;= 2.0
- [Memcached](http://memcached.org/) &gt;= 1.4

### Dependecies

[](#dependecies)

Big parts of all libraries will work with PHP 5.3. However since I am transitioning to PHP 5.4 you will find partly PHP 5.4 only code. This will grow depending how much time I find. Find all dependencies below:

- PHP &gt;= 5.4.8
- MySQL: [EasyPDO](https://github.com/fightbulc/easy_pdo)
- Redis: [phpiredis](https://github.com/nrk/phpiredis) (PHP bindings) for [hiredis](https://github.com/redis/hiredis) (C-Client for Redis)
- Couchbase: [PHP Client Library](http://www.couchbase.com/develop/php/current) with [C-Client library](https://github.com/couchbase/libcouchbase)
- Memcached: [libevent](http://libevent.org/)

Installing
----------

[](#installing)

You can install Simplon/Db either via package download from github or via composer install. I encourage you to do the latter:

```
{
  "require": {
    "simplon/db": "1.1.0"
  }
}
```

Depending on which database you would like to use pay attention to the above listed dependencies.

1. Usage MySQL
--------------

[](#1-usage-mysql)

Lets do some coding given that all desired databases and its dependencies were installed.

### 1.1 MySQL connection

[](#11-mysql-connection)

Lets create a MySQL connection instance:

```
require __DIR__ . '/../vendor/autoload.php';

// connect to server "localhost", db "test" with user/pass "rootuser/rootuser"
$dbInstance = Mysql::Instance('localhost', 'test', 'rootuser', 'rootuser');
```

Another way to create a mysql instance is via the `DbInstance class`. This class creates the instance and holds it as Singleton throughout runtime within a pool of other connections - in case you have to keep more than one connection:

```
require __DIR__ . '/../vendor/autoload.php';

// connect to server "localhost", db "test" with user/pass "rootuser/rootuser"
$dbInstance = \Simplon\Db\DbInstance::MySQL('localhost', 'test', 'rootuser', 'rootuser');
```

### 1.2 MySQL query

[](#12-mysql-query)

When querying a database we have again two options. The first option is to access the database directly via [EasyPDO](https://github.com/fightbulc/easy_pdo) which is a PDO wrapper:

```
require __DIR__ . '/../vendor/autoload.php';

// connect to server "localhost", db "test" with user/pass "rootuser/rootuser"
$dbInstance = \Simplon\Db\DbInstance::MySQL('localhost', 'test', 'rootuser', 'rootuser');

// ##############################################

// query
$results = $dbInstance->FetchAll('SELECT * FROM foobar WHERE ekey = :key', ['key' => 'BB']);

// dumps assoc. array of FALSE when fails
var_dump($results);
```

The other option requires the use of the `SqlManager class`. In order to use this class we need to pass a builder pattern class, `SqlQueryBuilder`, to communicate with our database. What advantage does that offer? Well in case that we want to do more things with our query before sending it off we encapsule it as an object within the `SqlQueryBuilder`. From there on we could pass it throughout our application to add more data or alike before sending the query finally to the database:

```
require __DIR__ . '/../vendor/autoload.php';

// connect to server "localhost", db "test" with user/pass "rootuser/rootuser"
$dbInstance = \Simplon\Db\DbInstance::MySQL('localhost', 'test', 'rootuser', 'rootuser');

// ##############################################

// create SqlManager instance
$sqlManager = new \Simplon\Db\SqlManager($dbInstance);

// query builder
$sqlQuery = (new \Simplon\Db\SqlQueryBuilder())
    ->setQuery('SELECT * FROM foobar WHERE ekey = :key')
    ->setConditions(['key' => 'BB']);

// query
$results = $sqlManager->fetchAll($sqlQuery);

// dumps assoc. array of FALSE when fails
var_dump($results);
```

What both options have in common are the named parameters `ekey = :key` which are identified by the conditions- / data-array keys.

### 1.3 MySQL insert/update

[](#13-mysql-insertupdate)

The way how to insert/update datasets differs for both options. Again see the following examples for better understanding:

```
require __DIR__ . '/../vendor/autoload.php';

// connect to server "localhost", db "test" with user/pass "rootuser/rootuser"
$dbInstance = \Simplon\Db\DbInstance::MySQL('localhost', 'test', 'rootuser', 'rootuser');

// ##############################################

// query: inserts one new row
$data = ['id' => NULL, 'ekey' => 'DD'];
$dbInstance->ExecuteSQL('INSERT INTO foobar VALUES (:id, :ekey)', $data);

// ##############################################

// query update
$data = ['id' => 5, 'ekey' => 'FF'];
$dbInstance->ExecuteSQL('UPDATE INTO foobar VALUES (:ekey) WHERE id = :id', $data);
```

Here goes our SqlManager solution with SqlQueryBuilder:

```
require __DIR__ . '/../vendor/autoload.php';

// connect to server "localhost", db "test" with user/pass "rootuser/rootuser"
$dbInstance = \Simplon\Db\DbInstance::MySQL('localhost', 'test', 'rootuser', 'rootuser');

// ##############################################

// query: inserts one new row
$data = ['id' => NULL, 'ekey' => 'DD'];

$sqlQuery = (new SqlQueryBuilder())
->setTableName('foobar')    // define the table name
->setData($data);           // set data (keys = database column name)

$sqlManager->insert($sqlQuery);

// ##############################################

// query update
$conds = ['id' => 5];
$data = ['ekey' => 'FF'];

$sqlQuery = (new SqlQueryBuilder())
->setTableName('foobar')    // define the table name
->setConditions($conds)     // set conditions
->setData($data);           // set data (keys = database column name)

$sqlManager->update($sqlQuery);
```

Difference is that for the latter method we don't need to write any repetitive SQL which in turn results in better maintenance and general code overview.

### 1.4 MySQL remove datasets

[](#14-mysql-remove-datasets)

From time to time we also need to remove a couple of datasets. Again, two examples:

```
require __DIR__ . '/../vendor/autoload.php';

// connect to server "localhost", db "test" with user/pass "rootuser/rootuser"
$dbInstance = \Simplon\Db\DbInstance::MySQL('localhost', 'test', 'rootuser', 'rootuser');

// ##############################################

// query
$dbInstance->ExecuteSQL('DELETE FROM foobar WHERE id = :id', ['id' => 5]);
```

SqlManager with SqlQueryBuilder:

```
require __DIR__ . '/../vendor/autoload.php';

// connect to server "localhost", db "test" with user/pass "rootuser/rootuser"
$dbInstance = \Simplon\Db\DbInstance::MySQL('localhost', 'test', 'rootuser', 'rootuser');

// ##############################################

// query
$sqlQuery = (new SqlQueryBuilder())
->setTableName('foobar')        // define the table name
->setConditions(['id' => 5]);   // set conditions

$sqlManager->remove($sqlQuery);
```

### 1.5 MySQL summary: direct access

[](#15-mysql-summary-direct-access)

- Connect (both options are valid):
    - `Mysql::Instance(HOST, DB, USER, PASSWORD)`
    - `\Simplon\Db\DbInstance::MySQL(HOST, DB, USER, PASSWORD)`
        - Returns: DbInstance
- Fetch all found data:
    - `DbInstance->FetchAll(QUERY, CONDS)`
        - Returns an assoc. array
    - `DbInstance->FetchArray(QUERY, CONDS)`
        - Returns an array
    - `DbInstance->FetchObject(QUERY, CONDS)`
        - Returns an object
- Fetch by steps:
    - `DbInstance->Fetch(QUERY, CONDS)`
        - Returns an iterator pointer which is essential for very big result sets
- Fetch one column value:
    - `DbInstance->FetchValue(QUERY, CONDS)`
        - Returns the first selected column
- Insert data:
    - `DbInstance->ExecuteSql(INSERT-QUERY, DATA)`
        - Returns insert-id or null. FALSE when failed
- Update data:
    - `DbInstance->ExecuteSql(UPDATE-QUERY, DATA)`
        - Returns FALSE when failed
- Remove data:
    - `DbInstance->ExecuteSql(DELETE-QUERY, DATA)`
        - Returns FALSE when failed

### 1.6 MySQL summary: access via SqlManager with SqlQueryBuilder

[](#16-mysql-summary-access-via-sqlmanager-with-sqlquerybuilder)

- Connect (both options are valid):
    - `Mysql::Instance(HOST, DB, USER, PASSWORD)`
    - `\Simplon\Db\DbInstance::MySQL(HOST, DB, USER, PASSWORD)`
        - Returns: DbInstance
- SqlManager instance:
    - `SqlManager = new \Simplon\Db\SqlManager(DbInstance)`
- Fetch all found data:
    - `SqlQueryBuilder = (new SqlQueryBuilder)->setQuery(QUERY)->setConditions(CONDS)`
    - `SqlManager->fetchAll(SqlQueryBuilder)`
        - Returns an assoc. array
- Fetch by steps:
    - `SqlQueryBuilder = (new SqlQueryBuilder)->setQuery(QUERY)->setConditions(CONDS)`
    - `SqlManager->fetchCursor(SqlQueryBuilder)`
        - Returns an iterator pointer which is essential for very big result sets
- Fetch one column value:
    - `SqlQueryBuilder = (new SqlQueryBuilder)->setQuery(QUERY)->setConditions(CONDS)`
    - `SqlManager->fetchColumn(SqlQueryBuilder)`
        - Returns the first selected column
- Insert data:
    - `SqlQueryBuilder = (new SqlQueryBuilder)->setTableName(TABLENAME)->setData(DATA)`
    - `SqlManager->insert(SqlQueryBuilder)`
        - Returns insert-id or null. FALSE when failed
- Update data:
    - `SqlQueryBuilder = (new SqlQueryBuilder)->setTableName(TABLENAME)->setConditions(CONDS)->setData(DATA)`
    - `SqlManager->update(SqlQueryBuilder)`
        - Returns FALSE when failed
- Remove data:
    - `SqlQueryBuilder = (new SqlQueryBuilder)->setTableName(TABLENAME)->setConditions(CONDS)`
    - `SqlManager->remove(SqlQueryBuilder)`
        - Returns FALSE when failed

2. Usage Redis
--------------

[](#2-usage-redis)

Work in progress ...

Changelog
=========

[](#changelog)

Version 1.1.0
-------------

[](#version-110)

- Refactored Redis library since it had &gt;3000 LOC
- Redis library has been seperated by its commands
- RedisManager offers references to all command classes

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity67

Established project with proven stability

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~23 days

Recently: every ~35 days

Total

18

Last Release

4562d ago

Major Versions

0.5.1 → 1.0.02013-03-04

PHP version history (3 changes)0.5.0PHP &gt;=5.3.3

1.0.0PHP &gt;=5.4.8

1.0.2PHP &gt;=5.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/451061?v=4)[Tino Ehrich](/maintainers/fightbulc)[@fightbulc](https://github.com/fightbulc)

---

Tags

mysqlpdoredisdbmemcachedcouchbase

### Embed Badge

![Health badge](/badges/simplon-db/health.svg)

```
[![Health](https://phpackages.com/badges/simplon-db/health.svg)](https://phpackages.com/packages/simplon-db)
```

###  Alternatives

[phpfastcache/phpfastcache

PHP Abstract Cache Class - Reduce your database call using cache system. Phpfastcache handles a lot of drivers such as Apc(u), Cassandra, CouchBase, Couchdb, Dynamodb, Firestore, Mongodb, Files, (P)redis, Leveldb, Memcache(d), Ravendb, Ssdb, Sqlite, Wincache, Xcache, Zend Data Cache.

2.4k5.0M130](/packages/phpfastcache-phpfastcache)[matthiasmullie/scrapbook

Scrapbook is a PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APCu, SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

3212.5M32](/packages/matthiasmullie-scrapbook)[malkusch/lock

Mutex library for exclusive code execution.

9459.6M27](/packages/malkusch-lock)[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4572.9M34](/packages/aura-sqlquery)[envms/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

925511.7k13](/packages/envms-fluentpdo)[apix/cache

A thin PSR-6 cache wrapper with a generic interface to various caching backends emphasising cache taggging and indexing to Redis, Memcached, PDO/SQL, APC and other adapters.

114542.8k6](/packages/apix-cache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
