PHPackages                             webx/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. webx/db

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

webx/db
=======

1.1.8(9y ago)03081MITPHP

Since Oct 25Pushed 9y ago1 watchersCompare

[ Source](https://github.com/niclaslindberg/webx-db)[ Packagist](https://packagist.org/packages/webx/db)[ RSS](/packages/webx-db/feed)WikiDiscussions master Synced today

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

WebX-Db - Mysql PHP library
===========================

[](#webx-db---mysql-php-library)

Main features and design goals of webx-db:

- Name based auto-escaped parametrization.
- Nested transaction support (`savepoint X` and `rollback to savepoint X`).
- Key violation exception with name of violated key.
- Easy to use in IOC based designs.
- Light weight.

Installing
----------

[](#installing)

```
* Packagist: webx-db

```

Getting started
---------------

[](#getting-started)

```
    use WebX\Db\Db;
    use WebX\Db\Impl\DbImpl;

    $db = new DbImpl([
        "user" => "mysqlUser",
        "password" => "mysqlPassword",
        "database" => "mysqlDatabase"
    ]);

    //or

    $db = new DbImpl($mysqli); //Instance of mysqli
```

### A simple insert

[](#a-simple-insert)

```
    $person = ["first"=>"Alex", "last"=>"Morris","email"=>"am@domain.com"];
    $db->execute("INSERT INTO people (first,last,email,phone) VALUES(:first,:last,:email,:phone)", $person);
    //SQL: INSERT INTO people (first,last,email,phone) VALUES('Alex','Morris','am@domain',NULL);

    $id = $db->insertId();          //The value of the autogenerated primary key.
```

### A simple select

[](#a-simple-select)

```
    foreach($db->allRows("SELECT * FROM table") as $row) {
        echo($row->string('first'));
        echo($row->string('last'));
    }
```

### Insert with key violations

[](#insert-with-key-violations)

```
    $person = [...];
    try {
        $db->execute("INSERT INTO people (first,last,email) VALUES(:first,:last,:email)", $person);
        echo("Person registered");
    } catch(DbKeyException $e) {
        if($e->key()==='emailKey') { // Name of the defined unique key in MySQL
            echo("The {$person->string('email')} is already registered";
        } else {
            echo("Some other key violation occured.");
        }
    }
```

### Nested transactions

[](#nested-transactions)

```
    $db->startTx();
    $db->execute("INSERT INTO table (col) VALUES('1')");

        $db->startTx();
        $db->execute("INSERT INTO table (col) VALUES('2')"); // Will not be commited
        $db->rollbackTx();

        $db->startTx();
        $db->execute("INSERT INTO table (col) VALUES('3')");
        $db->commitTx();

    $db->commitTx();
```

Note: If an outer tx is rolled back all its inner txs are also rolled back.

### Wrap transactions in a closure

[](#wrap-transactions-in-a-closure)

The execution of the closure is wrapped in a `startTx()` `commitTx()|rollbackTx()` depending on if the closure throws an exception or not.

```
    $db->executeInTx(function($db) { // The db instance must be passed as the only argument to closure.
        $db->execute("INSERT INTO table (col) VALUES('1')");
        $db->execute("INSERT INTO table (col) VALUES('2')");

        if(true) { //Now the transaction will be rolled back
            throw new \Exception("An error occured");
        }
    });
```

How to run tests
----------------

[](#how-to-run-tests)

In the root of the project:

`composer install`

`phpunit -c tests`

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~33 days

Recently: every ~49 days

Total

10

Last Release

3601d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/bba7dfe2d222db40ed75a5eb27347e437d3d1129d15c6822d0e46be6ee736df0?d=identicon)[niclaslindberg](/maintainers/niclaslindberg)

---

Top Contributors

[![niclaslindberg](https://avatars.githubusercontent.com/u/4067396?v=4)](https://github.com/niclaslindberg "niclaslindberg (29 commits)")

---

Tags

databasemysql

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[rah/danpu

Zero-dependency MySQL dump library for easily exporting and importing databases

62414.3k11](/packages/rah-danpu)

PHPackages © 2026

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