PHPackages                             liquidbox/silex-pdo - 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. liquidbox/silex-pdo

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

liquidbox/silex-pdo
===================

A PDO service provider for the Silex micro-framework

v2.0.0(8y ago)26.2k1[1 PRs](https://github.com/liquidbox/silex-pdo/pulls)MITPHP

Since Nov 24Pushed 7y ago1 watchersCompare

[ Source](https://github.com/liquidbox/silex-pdo)[ Packagist](https://packagist.org/packages/liquidbox/silex-pdo)[ RSS](/packages/liquidbox-silex-pdo/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

[![GitHub release](https://camo.githubusercontent.com/1dcfecb9bef3e14171941d065cb68ba2060b7ade03cb3e4286c963ec0617233d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6c6971756964626f782f73696c65782d70646f2e737667)](https://github.com/liquidbox/silex-pdo/releases)[![license](https://camo.githubusercontent.com/251625cbc4d456b44876d830e91d35eea2a7421dfefba5524bef0d859dce311f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c6971756964626f782f73696c65782d70646f2e737667)](LICENSE)[![Build Status](https://camo.githubusercontent.com/132851bb13e9603cef05ddf09df369bdeecc66a0f6be217f90c4d4711ae409aa/68747470733a2f2f7472617669732d63692e6f72672f6c6971756964626f782f73696c65782d70646f2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/liquidbox/silex-pdo)[![Code Coverage](https://camo.githubusercontent.com/51a7027ddf581c11bbf7321ac4baa67ee4d2aec6df95d828047c69c92b3fc0eb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6971756964626f782f73696c65782d70646f2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/liquidbox/silex-pdo/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e6b284c06e59c4cbd39aed60cb684d97a605de30fe2ebd90346b97edcee27f66/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6971756964626f782f73696c65782d70646f2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/liquidbox/silex-pdo/?branch=master)[![Packagist](https://camo.githubusercontent.com/5031bc071099ec5aebc3f0bd5d5f67b5a3f4a3e0091dd2741c69c8ca6187ca8d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6971756964626f782f73696c65782d70646f2e737667)](https://packagist.org/packages/liquidbox/silex-pdo)

You are reading the documentation for Silex 2.x. Switch to the documentation for Silex [1.x](../v1.0.0/README.md).

PHP Data Objects
================

[](#php-data-objects)

The *PdoServiceProvider* provides integration with the [PHP Data Objects (PDO)](http://php.net/manual/intro.pdo.php) extension.

Parameters
----------

[](#parameters)

- **pdo.dsn** (optional): The Data Source Name, or DSN, contains the information required to connect to the database.
- **pdo.driver** (optional): The [PDO driver](http://php.net/manual/pdo.drivers.php) implementation to use.
- **pdo.connection** (optional): A collection of driver-specific connection parameters for specifying the connection string.
- **pdo.username** (optional): The user name for the DSN string.
- **pdo.password** (optional): The password for the DSN string.
- **pdo.options** (optional): A collection of driver-specific connection options.
- **pdo.attributes** (optional): A collection of attributes to set.

The parameters `pdo.driver` and `pdo.connection` are ignored if `pdo.dsn` is set.

Services
--------

[](#services)

- **pdo**: The [`PDO`](http://php.net/manual/class.pdo.php) connection instance. The main way of interacting with PDO.
- **pdo.connections**: The collection of PDO connection instances. See section on [using multiple databases](#using-multiple-databases) for details.
- **pdo.connect**: Factory for `PDO` connection instances.

Registering
-----------

[](#registering)

**Example #1 Connecting to MySQL**

```
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array(
    'pdo.dsn' => 'mysql:host=localhost;dbname=test',
    'pdo.username' => $user,
    'pdo.password' => $passwd,
));

// or

$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array(
    'pdo.connection' => array(
        'host'   => 'localhost',
        'dbname' => 'test'
    ),
    'pdo.username' => $user,
    'pdo.password' => $passwd,
));
```

The two registered connections are equivalent.

**Example #2 Connecting to SQLite**

```
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array(
    'pdo.dsn' => 'sqlite::memory:',
));

// or

$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array(
    'pdo.driver' => 'sqlite',
    'pdo.connection' => ':memory:',
));
```

**Example #3 Using Doctrine service names**

```
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider('db', 'dbs'), array(
    'pdo.driver' => 'pdo_pgsql',
    'pdo.connection' => array(
        'host'   => 'localhost',
        'dbname' => 'test',
    ),
    'pdo.username' => $user,
    'pdo.password' => $passwd,
));
```

The services `pdo` and `pdo.connections` will be renamed `db` and `dbs` respectively.

Add PDO as a dependency:

```
composer require liquidbox/silex-pdo:^2.0
```

Usage
-----

[](#usage)

**Example #1 Demonstrate query**

```
$sql = 'SELECT name, color, calories FROM fruit ORDER BY name';

foreach ($app['pdo']->query($sql) as $row) {
    echo implode("\t", $row) . PHP_EOL;
}
```

**Example #2 Prepare an SQL statement with named parameters**

```
/* Execute a prepared statement by passing an array of values */
$sql = prepare($sql, array(
    \PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY,
));

$pdoStatement->execute(array(':calories' => 150, ':color' => 'red'));

$red = $pdoStatement->fetchAll();

$pdoStatement->execute(array(':calories' => 175, ':color' => 'yellow'));

$yellow = $pdoStatement->fetchAll();
```

Using multiple databases
------------------------

[](#using-multiple-databases)

The PDO provider can allow access to multiple databases. In order to configure the data sources, use **pdo.dsn** as an array of configurations where keys are connection names and values are parameters:

```
$app->register(new LiquidBox\Silex\Provider\PdoServiceProvider(), array(
    'pdo.dsn' => array(
        'mysql_read' => array(
            'connection' => array(
                'host'    => 'mysql_read.someplace.tld',
                'dbname'  => 'my_database',
                'charset' => 'utf8mb4',
            ),
            'username' => 'my_username',
            'password' => 'my_password',
        ),
        'mysql_write' => array(
            'connection' => array(
                'host'    => 'mysql_write.someplace.tld',
                'dbname'  => 'my_database',
                'charset' => 'utf8mb4',
            ),
            'username' => 'my_username',
            'password' => 'my_password',
        ),
    ),
));
```

The first registered connection is the default and can simply be accessed as you would if there was only one connection. Given the above configuration, these two lines are equivalent:

```
$app['pdo']->query('SELECT * FROM table')->fetchAll();

$app['pdo.connections']['mysql_read']->query('SELECT * FROM table')->fetchAll();
```

You can use different drivers for each connection.

```
$app->register(
    new LiquidBox\Silex\Provider\PdoServiceProvider(null, 'pdo.dbs'),
    array(
        'pdo.dsn' => array(
            'member_db' => array(
                'driver'     => 'pdo_pgsql',
                'connection' => array(
                    'host'    => 'member_data.someplace.tld',
                    'dbname'  => 'membership',
                ),
                'username' => $pgsql_user,
                'password' => $pgsql_passwd,
            ),
            'content_db' => array(
                'connection' => array(
                    'host'    => 'content_data.someplace.tld',
                    'dbname'  => 'media_info',
                    'charset' => 'utf8',
                ),
                'username' => $mysql_user,
                'password' => $mysql_passwd,
            ),
            'session_storage' => array('dsn' => 'sqlite::memory:'),
        ),
    )
);
```

This registers `$app['pdo.dbs']['member_db']`, `$app['pdo.dbs']['content_db']`, and `$app['pdo.dbs']['session_storage']` using PostgreSQL, MySQL, and SQLite drivers respectively.

Traits
------

[](#traits)

`LiquidBox\Silex\Application\PdoTrait` adds the following shortcut:

- **prepare**: Prepares a statement for execution and returns a statement object.

```
/* Execute a prepared statement by passing an array of values */
$pdoStatement = $app->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?');

$pdoStatement->execute(array(150, 'red'));

$red = $pdoStatement->fetchAll();

$pdoStatement->execute(array(175, 'yellow'));

$yellow = $pdoStatement->fetchAll();
```

For more information, check out the [official PDO documentation](http://php.net/manual/book.pdo.php).

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

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

Total

2

Last Release

3127d ago

Major Versions

v1.0.0 → v2.0.02017-11-30

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/17163576?v=4)[liquidBOX](/maintainers/liquidbox)[@liquidbox](https://github.com/liquidbox)

---

Top Contributors

[![ExplicitDev](https://avatars.githubusercontent.com/u/17163543?v=4)](https://github.com/ExplicitDev "ExplicitDev (2 commits)")

---

Tags

phpdatabasemysqlsqlitepostgresqlpdoserviceproviderodbcsilex

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/liquidbox-silex-pdo/health.svg)

```
[![Health](https://phpackages.com/badges/liquidbox-silex-pdo/health.svg)](https://phpackages.com/packages/liquidbox-silex-pdo)
```

###  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)[dibi/dibi

Dibi is Database Abstraction Library for PHP

5013.9M129](/packages/dibi-dibi)[aura/sqlquery

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

4563.1M37](/packages/aura-sqlquery)[popphp/pop-db

Pop Db Component for Pop PHP Framework

1815.7k12](/packages/popphp-pop-db)[ramadan/easy-model

A Laravel package for enjoyably managing database queries.

111.6k](/packages/ramadan-easy-model)

PHPackages © 2026

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