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

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

elbucho/database
================

Utility wrapper for the PDO subsystem

1.2.1(4y ago)013MITPHPPHP &gt;=7.2CI failing

Since Jun 20Pushed 4y agoCompare

[ Source](https://github.com/elbucho/database)[ Packagist](https://packagist.org/packages/elbucho/database)[ RSS](/packages/elbucho-database/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (5)Versions (9)Used By (0)

elbucho/database
================

[](#elbuchodatabase)

This project provides a utility wrapper around PDO requests for MySQL. It allows you to lazy-load multiple different database connections in one callable object, and gives you a streamlined access to PDO's methods.

Configuration
-------------

[](#configuration)

The Database class is instantiated by passing it an Elbucho\\Config object with one or more DSNs in it. See the elbucho/config documentation for instructions on how to place and read in your config files.

### DSNs

[](#dsns)

Database connections must be stored in the "dsns" key of your Config object, and each must have the below required keys. You can include multiple connections in one Config object.

The required keys for each database connection are:

- host =&gt; This is the hostname (eg. localhost)
- dbname =&gt; The name of the database you're connecting to
- user =&gt; Your database connection's username
- pass =&gt; Your database connection's password

Additionally, the optional key "port" can be specified with the port number your database server is running on. This defaults to 3306.

### Default Handle

[](#default-handle)

If your config file only contains information for one database connection, the Database object will set the handle for this connection to "default". If you have multiple connections, each connection must be prefaced with the handle name for that connection.

You can override the default handle name by specifying a 'default\_handle' key in your config:

```
default_handle: foobar

```

Below are two sample .yml files. This first one shows a singular connection, and it will be automatically assigned the default handle.

```
dsns:
    host:   localhost
    port:   3307
    dbname: app_data
    user:   app_user
    pass:   app_password

```

This .yml file shows multiple connections (eg. a dev and production db server):

```
dsns:
    dev:
        host:   localhost
        port:   3306
        dbname: app_dev
        user:   dev_user
        pass:   dev_password

    prod:
        host:   10.20.1.101
        port:   3308
        dbname: app_prod
        user:   prod_user
        pass:   prod_password

```

This will create two handles in the $database object: "dev" and "prod".

Querying the Database
---------------------

[](#querying-the-database)

Once your config file is set, you can instantiate a database object and query it in one of two ways:

### $database-&gt;query():

[](#database-query)

This method takes up to 3 arguments: your query itself, any parameters you wish to pass to the MySQL engine, and the handle to query (defaults to "default"). It returns an array of results or throws a \\PDOException if there was an issue with the query.

```
$database = new Database($config);

$results = $database->query('SELECT * FROM users`, [], 'prod');

foreach ($results as $user) {
    ...
}

```

### $database-&gt;exec():

[](#database-exec)

This method takes the same 3 arguments as query(), but does not return results. It is useful for passing commands to MySQL that you do not require a return for:

```
$database->exec('SET NAMES utf8mb4', [], 'prod');

```

```
$newUser = ['johnSmith', 'john.smith@company.org'];

$database->exec('INSERT INTO users (username, email) VALUES (?,?)', $newUser, 'prod');

```

Fetching last insert ID
-----------------------

[](#fetching-last-insert-id)

If you've just inserted a row or rows into your database, you can get the last insert id by typing:

```
$lastId = $database->getLastInsertId('prod');

var_dump($lastId);

// int(12345)

```

Again, if the handle is left out, it will use whatever your default handle is set to.

Fetching the number of rows affected by the previous query
----------------------------------------------------------

[](#fetching-the-number-of-rows-affected-by-the-previous-query)

If you've just updated / inserted / deleted one or more rows in your database, you can get the count of rows affected by using:

```
$rowCount = $database->getRows('prod');

var_dump($rowCount);

// int(23456)

```

Similar to `getLastInsertId()`, if you leave out the handle, it will use whatever your default handle is set to.

Setting Attributes
------------------

[](#setting-attributes)

You can set PDO attributes on a given connection via the setAttribute method. This works identically to the PDO setAttribute method, with the exception that a handle is also passed:

```
$attribute = \PDO::ATTR_ERRMODE;
$value = \PDO::ERRMODE_EXCEPTION;
$handle = 'default';

$database->setAttribute($attribute, $value, $handle)

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

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

Recently: every ~147 days

Total

8

Last Release

1565d ago

PHP version history (2 changes)1.0.0PHP &gt;=7.1

1.2.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/93a0e79682cd43644eb1812ae1706fb8844b1b595fa571bc5f8854af2a670d1a?d=identicon)[elbucho](/maintainers/elbucho)

---

Top Contributors

[![elbucho](https://avatars.githubusercontent.com/u/4545458?v=4)](https://github.com/elbucho "elbucho (13 commits)")

---

Tags

databasepdo

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[doctrine/dbal

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

9.7k578.4M5.6k](/packages/doctrine-dbal)[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k5.5M69](/packages/ifsnop-mysqldump-php)[nette/database

💾 Nette Database: layer with a familiar PDO-like API but much more powerful. Building queries, advanced joins, drivers for MySQL, PostgreSQL, SQLite, MS SQL Server and Oracle.

5656.7M234](/packages/nette-database)[dibi/dibi

Dibi is Database Abstraction Library for PHP

5013.8M120](/packages/dibi-dibi)[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)

PHPackages © 2026

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