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

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

phlib/db
========

MySQL PDO DB Adapter. PDO with some extra good stuff specifically for MySQL.

3.0.0(1y ago)557.9k—9.7%[2 issues](https://github.com/phlib/db/issues)5LGPL-3.0PHPPHP ^8.1CI passing

Since Apr 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/phlib/db)[ Packagist](https://packagist.org/packages/phlib/db)[ RSS](/packages/phlib-db/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (13)Used By (5)

phlib/db
========

[](#phlibdb)

[![Code Checks](https://camo.githubusercontent.com/fd0ecdf742583d3bba7a2464a22a7da46ca6a45ecfe708fc8477c75c8662ce4a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f70686c69622f64622f636f64652d636865636b732e796d6c3f6c6f676f3d676974687562)](https://github.com/phlib/db/actions/workflows/code-checks.yml)[![Codecov](https://camo.githubusercontent.com/e2be15b9eb1eb41685e3f8aa7ee9efade9513676196eae60a67edbe1aa283325/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f70686c69622f64622e7376673f6c6f676f3d636f6465636f76)](https://codecov.io/gh/phlib/db)[![Latest Stable Version](https://camo.githubusercontent.com/4c777482e14fd022b70687a80a62326c20fb16f46d1b93cea51c899bb98147c2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70686c69622f64622e7376673f6c6f676f3d7061636b6167697374)](https://packagist.org/packages/phlib/db)[![Total Downloads](https://camo.githubusercontent.com/d413e4515f5c8aa7680c7a523de9f39670ad2277c5922d0c8e646fafbecf5c1b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70686c69622f64622e7376673f6c6f676f3d7061636b6167697374)](https://packagist.org/packages/phlib/db)[![Licence](https://camo.githubusercontent.com/6354727e729b1f022538ac788c83e9268abc1da2c26e255f28987ee1b168d117/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f70686c69622f64622e737667)](https://camo.githubusercontent.com/6354727e729b1f022538ac788c83e9268abc1da2c26e255f28987ee1b168d117/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f70686c69622f64622e737667)

MySQL PDO DB Adapter. PDO with some extra good stuff specifically for MySQL.

What this DB wrapper offers over others.

- Database switching method
- Unified unknown database exception
- Capturing invalid SQL statements exception
- Automatic reconnect useful for long running processes (server has gone away)
- Connection retries on failed to connect
- Connection cloning
- Useful additional methods for:
    - timezone
    - buffering
    - quoting (tables, columns, values)
    - select
    - insert
    - update
    - delete

Install
-------

[](#install)

Via Composer

```
$ composer require phlib/db
```

or

```
"require": {
    "phlib/db": "*"
}
```

Basic Usage
-----------

[](#basic-usage)

```
$config = [
    'host' => 'localhost',
    'username' => 'myuser',
    'password' => 'mypassword',
    'dbname' => 'mydatabase'
];
$db = new \Phlib\Db\Adapter($config);
```

```
$table = $db->quoteIdentifier('mytable');
/* @var $stmt \PDOStatement */
$stmt = $db->query("SELECT * FROM $table WHERE id = ?", [$rowId]);
```

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

[](#configuration)

NameTypeRequiredDefaultDescription`host`*String*YesHostname or IP address.`username`*String*No`''`Username to connect to server.`password`*String*No`''`Password to connect to server.`port`*Integer*NoPort to connect to server.`dbname`*String*NoDatabase name to use.`charset`*String*No`'utf8mb4'`Sets the character to use on the connection.`timezone`*String*No`'+0:00'`Sets the timezone to use on the connection. Values: `'system'`, '`+\-dd:dd`' or 'timezone'. [MySQL Manual](http://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html)`timeout`*Integer*No`2`Sets the connection timeout. Range from 0 to 120.`retryCount`*Integer*No`0`Sets how many times to try to reconnect to the DB server after unsuccessful connection attempts. Range from 0 to 10.`attributes`*Array*No`[]`Driver-specific options used when connecting, e.g. `\PDO::ATTR_*`API
---

[](#api)

The following section documents the less obvious API's. Most methods are doc blocked and are self explanatory.

`Adapter::__clone`

This is useful when you're dealing with the results of one query while inserting as both operations can not be done on the same connection.

```
$db2 = (clone)$db1;
```

`Adapter` Buffering

This is useful when requesting large amounts of data from the DB server. By default, PDO will pull all the results back and hold the results in memory even for `fetch()` calls. With large result sets this causes out of memory problems. To stop PDO pulling the results back turn off buffering.

```
if ($db->isBuffered()) {
    $db->disableBuffering();
}
```

Exceptions
----------

[](#exceptions)

All Phlib Db Exceptions implement the `\Phlib\Db\Exception\Exception` interface.

```
try {
    $db = new \Phlib\Db\Adapter($config);
    $result = $db->query($sql, $bind);
} catch (\Phlib\Db\Exception\Exception $e) {
    $this->logException($e);
}
```

### Hierarchy

[](#hierarchy)

```
+-- \Exception
|  +-- \InvalidArgumentException
|  |  +-- \Phlib\Db\Exception\InvalidArgumentException
|  +-- \RuntimeException
|  |  +-- \PDOException
|  |  |  +-- \Phlib\Db\Exception\RuntimeException
|  |  |  |  +-- \Phlib\Db\Exception\UnknownDatabaseException
|  |  |  |  +-- \Phlib\Db\Exception\InvalidQueryException
```

### Invalid Query

[](#invalid-query)

The `InvalidQueryException` has special methods for retrieving the query and associated bind parameters. The message recorded for exception includes these details but the methods allow a clean way of extracting them.

- `getQuery`
- `getBindData`

Known Issues
------------

[](#known-issues)

Setting the connection from outside the class will cause odd behaviour.

```
$pdo = new \PDO('mysql:host=localhost');
$db = new \Phlib\Db\Adapter();
$db->setConnection($pdo);
$config = $db->getConfig(); // config is an empty array

$db->reconnect(); // throws InvalidArgumentException missing host param.
```

License
-------

[](#license)

This package is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see .

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 98.2% 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 ~293 days

Recently: every ~322 days

Total

12

Last Release

461d ago

Major Versions

0.0.5 → 1.0.02017-04-10

1.2.0 → 2.0.02021-08-07

2.2.0 → 3.0.02025-02-11

PHP version history (4 changes)0.0.1PHP ^5.5 || ^7.0

1.0.0PHP ^5.6 || ^7.0

2.0.0PHP ^7.4 || ^8.0

3.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/135b7ddf9ec91c412e1b18174f81d1ad2bef66e732624195c156717c96b13731?d=identicon)[letssurf](/maintainers/letssurf)

![](https://www.gravatar.com/avatar/10c53cdcfb3a6d299820aecb993521cc1a972baa09fd8f31d2468908cded7e1d?d=identicon)[chrisminett](/maintainers/chrisminett)

---

Top Contributors

[![chrisminett](https://avatars.githubusercontent.com/u/1084019?v=4)](https://github.com/chrisminett "chrisminett (109 commits)")[![jdempster](https://avatars.githubusercontent.com/u/10297?v=4)](https://github.com/jdempster "jdempster (2 commits)")

---

Tags

mysqladapterdb

###  Code Quality

TestsPHPUnit

Code StyleECS

### Embed Badge

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

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

###  Alternatives

[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)[lichtner/fluentpdo

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

921274.8k6](/packages/lichtner-fluentpdo)[fpdo/fluentpdo

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

921244.9k7](/packages/fpdo-fluentpdo)[danielme85/laravel-log-to-db

Custom Laravel Log channel handler that can store log events to SQL or MongoDB databases. Uses Laravel native logging functionality.

135934.5k1](/packages/danielme85-laravel-log-to-db)[colshrapnel/safemysql

A real safe and convenient way to handle MySQL queries.

400103.5k4](/packages/colshrapnel-safemysql)

PHPackages © 2026

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