PHPackages                             anax/database - 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. anax/database

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

anax/database
=============

Anax Database module, to work with databases.

v2.3.1(5y ago)015.2k↓91.6%9[2 issues](https://github.com/canax/database/issues)20MITPHPPHP &gt;=7.2CI failing

Since Mar 31Pushed 5y ago1 watchersCompare

[ Source](https://github.com/canax/database)[ Packagist](https://packagist.org/packages/anax/database)[ Docs](https://dbwebb.se/anax)[ RSS](/packages/anax-database/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (1)Versions (29)Used By (20)

Anax Database
=============

[](#anax-database)

[![Latest Stable Version](https://camo.githubusercontent.com/9452509eaad6017811c41153abbdbcb4bf69becdd5e496856545817527696b11/68747470733a2f2f706f7365722e707567782e6f72672f616e61782f64617461626173652f762f737461626c65)](https://packagist.org/packages/anax/database)[![Join the chat at https://gitter.im/canax/database](https://camo.githubusercontent.com/afd22dbe5c68c5e6a291755a0e825d6eff0867ab3cb632971ac19c2d12f328a8/68747470733a2f2f6261646765732e6769747465722e696d2f63616e61782f64617461626173652e737667)](https://gitter.im/canax/database?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

[![Build Status](https://camo.githubusercontent.com/940f203cc8b65249dc0fdab57dacf98644f5a694b1e19fcf29394bd1962075ed/68747470733a2f2f7472617669732d63692e6f72672f63616e61782f64617461626173652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/canax/database)[![CircleCI](https://camo.githubusercontent.com/24d5c6d9678b04b81566010880a4037c9ce93bb5bf9136f85da5dd8b2a4cdf15/68747470733a2f2f636972636c6563692e636f6d2f67682f63616e61782f64617461626173652e7376673f7374796c653d737667)](https://circleci.com/gh/canax/database)

[![Build Status](https://camo.githubusercontent.com/e636e409f2b633119be19ac8710cae62626a160638c342dbfddd0d7e80c659c5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f64617461626173652f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/database/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f0b49b37d222b41c9958c3a4281e29638519263e0f5d4728b4c917dfbb9f2f3a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f64617461626173652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/database/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/f6dc0f70e79b6e70e2c16d15e01e825f8c299efb998f53000bbde505180b6207/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f64617461626173652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/database/?branch=master)

[![Maintainability](https://camo.githubusercontent.com/946792c3b282bfa243a3c0f47000b95d8fc9215667cc20de52be464a4bc936de/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f61623063346434373235363564393565363466662f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/canax/database/maintainability)[![Codacy Badge](https://camo.githubusercontent.com/10cc4009e476d29c979750f06bdeb0f0fb22bb812a0de7dfc9eafc9a43dab7e3/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3664666636303434643235363436653962636165613361333333313038646564)](https://www.codacy.com/app/mosbth/database?utm_source=github.com&utm_medium=referral&utm_content=canax/database&utm_campaign=Badge_Grade)

Anax Database module as a database abstraction layer (DBA) for wrapping PHP PDO with an additional layer of utilities and ease of use together with ability to use configuration file and attach into an Anax installation as a servide in $di.

The module is tested using MySQL and SQLite.

There are separate modules for a Database Query Builder in [`anax\database-query-builder`](https://github.com/canax/database-query-builder) and a Database Active Record in [`anax\database-active-record`](https://github.com/canax/database-active-record).

Table of content
----------------

[](#table-of-content)

- [Install](#Install)
- [Development](#Development)
- [Class, interface, trait](#class-interface-trait)
- [Exceptions](#exceptions)
- [Configuration file](#configuration-file)
- [DI service](#di-service)
- [Access as framework service](#access-as-framework-service)
- [Create a connection](#create-a-connection)
- [Perform a SELECT query](#perform-a-select-query)
- [Fetch versus FetchAll](#Fetch-versus-FetchAll)
- [FetchClass](#FetchClass)
- [FetchInto](#FetchInto)
- [Perform an INSERT, UPDATE, DELETE query](#perform-an-insert-update-delete-query)
- [Last insert id](#last-insert-id)
- [Row count, affected rows](#row-count-affected-rows)
- [Throw exception on failure](#throw-exception-on-failure)
- [Dependency](#Dependency)
- [License](#License)

Install
-------

[](#install)

You can install the module from [`anax/database` on Packagist](https://packagist.org/packages/anax/database) using composer.

```
composer require anax/database

```

You can then copy the default configuration files as a start.

```
# In the root of your Anax installation
rsync -av vendor/anax/database/config .

```

Development
-----------

[](#development)

To work as a developer you clone the repo and install the local environment through make. Then you can run the unit tests.

```
make install
make test

```

Class, interface, trait
-----------------------

[](#class-interface-trait)

The following classes exists.

Class, interface, traitDescription`Anax\Database\Database`Wrapper class for PHP PDO with enhanced error handling and extra utilities.`Anax\Database\DatabaseDebug`An alternative class that can be used for debugging database related issues.Exceptions
----------

[](#exceptions)

All exceptions are in the namespace `Anax\Database\Exception\`. The following exceptions exists and may be thrown.

ExceptionDescription`Exception`General module specific exception, for example when connection fail.Configuration file
------------------

[](#configuration-file)

This is a sample configuration file. It is usually stored in `config/database.php` when used together with Anax.

```
/**
 * Config file for Database.
 *
 * Example for MySQL.
 *  "dsn" => "mysql:host=localhost;dbname=test;",
 *  "username" => "test",
 *  "password" => "test",
 *  "driver_options"  => [
 *      \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
 *  ],
 *
 * Example for SQLite.
 *  "dsn" => "sqlite::memory:",
 *  "dsn" => "sqlite:$path",
 *
 */
return [
    "dsn"             => null,
    "username"        => null,
    "password"        => null,
    "driver_options"  => null,
    "fetch_mode"      => \PDO::FETCH_OBJ,
    "table_prefix"    => null,
    "session_key"     => "Anax\Database",
    "emulate_prepares" => false,

    // True to be very verbose during development
    "verbose"         => null,

    // True to be verbose on connection failed
    "debug_connect"   => false,
];
```

You can use if-statements within the configuration file to serve different configurations for local development environment, staging and or production environment.

DI service
----------

[](#di-service)

The database is created as a framework service within `$di`. The following is a sample on how the database service is created through `config/di/db.php`.

```
/**
 * Configuration file for database service.
 */
return [
    // Services to add to the container.
    "services" => [
        "db" => [
            "shared" => true,
            "callback" => function () {
                $db = new \Anax\Database\Database();

                // Load the configuration files
                $cfg = $this->get("configuration");
                $config = $cfg->load("database");

                // Set the database configuration
                $connection = $config["config"] ?? [];
                $db->setOptions($connection);

                return $db;
            }
        ],
    ],
];
```

The setup callback works like this.

1. The database object is created.
2. The configuration file is read.
3. The configuration is applied.

Access as framework service
---------------------------

[](#access-as-framework-service)

You can access the database module as a framework service.

```
$sql = "SELECT * FROM movie;";

# $app style
$app->db->connect();
$res = $app->db->executeFetchAll($sql);

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->executeFetchAll($sql);
```

Create a connection
-------------------

[](#create-a-connection)

You must connect to the database before using it.

You may call `$db->connect()` many times, the connection is however only made once, the first time, so it is safe to call the method several times.

```
# $app style
$app->db->connect();

# $di style
$di->get("db")->connect();
```

Perform a SELECT query
----------------------

[](#perform-a-select-query)

You connect and perform the query which returns a resultset.

```
$sql = "SELECT * FROM movie;";

# $app style
$app->db->connect();
$res = $app->db->executeFetchAll($sql);

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->executeFetchAll($sql);
```

The contents of `$res` is depending on the configuration key which default is set to `"fetch_mode" => \PDO::FETCH_OBJ,`.

You can also separate `executeFetchAll()` into two separate commands `execute()` and `fetchAll()`.

```
$sql = "SELECT * FROM movie;";

# $app style
$app->db->connect();
$res = $app->db->execute($sql)->fetchAll();

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->execute($sql)->fetchAll();
```

Fetch versus FetchAll
---------------------

[](#fetch-versus-fetchall)

With `fetchAll()` you fetch all matching rows in an array. When no matching rows are found you get en ampty array.

With `fetch()` you get the first item in the resultset. You may use this when your resultset only contain one row.

```
$sql = "SELECT * FROM movie WHERE id = 1;";

# $app style
$app->db->connect();
$res = $app->db->executeFetch($sql);

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->executeFetch($sql);
```

The content of `$res` is now an object of type `\StdClass` and you access the resultset columns by their name, for example `$res->id`.

FetchClass
----------

[](#fetchclass)

You can fetch the resultset into an object instantiated from a specified class.

```
$sql = "SELECT * FROM movie WHERE id = ?;";

# $app style
$app->db->connect();
$res = $app->db->executeFetchClass($sql, [1], "\Anax\SomeClass");

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->executeFetchClass($sql, [1], "\Anax\SomeClass");
```

The resultset is inserted into a new object of the class `"\Anax\SomeClass"`.

There is also `executeFetchAllClass()` which fetches an array of all matching rows as new objects of the class.

FetchInto
---------

[](#fetchinto)

You can fetch the resultset into an existing object as public properties.

```
$sql = "SELECT * FROM movie WHERE id = ?;";
$obj = new SomeClass();

# $app style
$app->db->connect();
$res = $app->db->executeFetchInto($sql, [1], $obj);

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->executeFetchClass($sql, [1], $obj);
```

The resultset is inserted into the object `$obj`.

Perform an INSERT, UPDATE, DELETE query
---------------------------------------

[](#perform-an-insert-update-delete-query)

These queries, that updates the database, uses `$db->execute()` and does not return a resultset.

```
$sql = "UPDATE movie SET title = ? WHERE id = ?;";

# $app style
$app->db->connect();
$app->db->execute($sql, ["Some title", 1]);

# $di style
$db = $di->get("db");
$db->connect();
$db->execute($sql, ["Some title", 1]);
```

Last insert id
--------------

[](#last-insert-id)

You can check the last inserted id when doing INSERT where the primary key is auto generated.

```
$sql = "INSERT INTO movie (title) VALUES (?);";

# $app style
$app->db->connect();
$app->db->execute($sql, ["Some title"]);
$id = $app->lastInsertId();

# $di style
$db = $di->get("db");
$db->connect();
$db->execute($sql, ["Some title"]);
$id = $db->lastInsertId();
```

Row count, affected rows
------------------------

[](#row-count-affected-rows)

You can check how many rows that are affected by the last INSERT, UPDATE, DELETE statement.

```
$sql = "DELETE FROM movie;";

# $app style
$app->db->connect();
$app->db->execute($sql);
$num = $app->rowCount();

# $di style
$db = $di->get("db");
$db->connect();
$db->execute($sql);
$num = $db->rowCount();
```

Throw exception on failure
--------------------------

[](#throw-exception-on-failure)

Exception are in general thrown as soon as something fails.

The exception is module specific `Anax\Database\Exception\Exception` and contains details from the error message from the PDO layer, either from the statement or from the PDO-object, depending on what type of error happens.

Dependency
----------

[](#dependency)

No particular dependencies. The module is usually used within an Anax installation but can also be used without Anax.

License
-------

[](#license)

This software carries a MIT license. See [LICENSE.txt](LICENSE.txt) for details.

```
 .
..:  Copyright (c) 2013 - 2018 Mikael Roos, mos@dbwebb.se

```

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community24

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 97.4% of commits — single point of failure

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 ~48 days

Recently: every ~171 days

Total

28

Last Release

2069d ago

Major Versions

v1.1.10 → v2.0.02018-10-02

PHP version history (3 changes)v1.0.2PHP &gt;=5.6

v2.0.0PHP ^7.2

v2.1.1PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/4cac244120f54c02a4fb3186b91323f3f0805dd9daefd0babeb4a82a19232faf?d=identicon)[mikael\_roos](/maintainers/mikael_roos)

---

Top Contributors

[![mosbth](https://avatars.githubusercontent.com/u/169550?v=4)](https://github.com/mosbth "mosbth (75 commits)")[![NiklasAurora](https://avatars.githubusercontent.com/u/11875601?v=4)](https://github.com/NiklasAurora "NiklasAurora (2 commits)")

---

Tags

databaseDatabase Access Layerdba

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/anax-database/health.svg)

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

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k595.8M6.5k](/packages/doctrine-dbal)[doctrine/orm

Object-Relational-Mapper for PHP

10.2k295.3M7.2k](/packages/doctrine-orm)[doctrine/doctrine-bundle

Symfony DoctrineBundle

4.8k249.9M3.9k](/packages/doctrine-doctrine-bundle)[doctrine/migrations

PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deploying changes to it. It is a very easy to use and a powerful tool.

4.8k212.9M507](/packages/doctrine-migrations)[doctrine/data-fixtures

Data Fixtures for all Doctrine Object Managers

2.8k141.0M561](/packages/doctrine-data-fixtures)[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.5k47.9M442](/packages/robmorgan-phinx)

PHPackages © 2026

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