PHPackages                             sw897/easydb - 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. sw897/easydb

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

sw897/easydb
============

Easy-to-use database abstraction

1.1(8y ago)016MITPHP

Since Nov 29Pushed 8y agoCompare

[ Source](https://github.com/sw897/easydb)[ Packagist](https://packagist.org/packages/sw897/easydb)[ RSS](/packages/sw897-easydb/feed)WikiDiscussions master Synced 3d ago

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

EasyDB - Simple Database Abstraction Layer
==========================================

[](#easydb---simple-database-abstraction-layer)

[![Build Status](https://camo.githubusercontent.com/2d4b0844321a49b2b7bcfd814f2e57dd9930e250ed8ab163a7edda323badb5f7/68747470733a2f2f7472617669732d63692e6f72672f70617261676f6e69652f6561737964622e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/paragonie/easydb)[![Latest Stable Version](https://camo.githubusercontent.com/874f5543cc0ef90e3cc6f533dd5a1346d3abbef86f11cb371cf878e4e796f709/68747470733a2f2f706f7365722e707567782e6f72672f70617261676f6e69652f6561737964622f762f737461626c65)](https://packagist.org/packages/paragonie/easydb)[![Latest Unstable Version](https://camo.githubusercontent.com/459592f55c19d7ac7594cc1272d03fa73f0052ec24223e3373f1e9db83b5ef82/68747470733a2f2f706f7365722e707567782e6f72672f70617261676f6e69652f6561737964622f762f756e737461626c65)](https://packagist.org/packages/paragonie/easydb)[![License](https://camo.githubusercontent.com/3e8ca94e318af3d0b57f631b9e0720b69fda9a5e5136d225a5ba8c5c1c103a12/68747470733a2f2f706f7365722e707567782e6f72672f70617261676f6e69652f6561737964622f6c6963656e7365)](https://packagist.org/packages/paragonie/easydb)[![Downloads](https://camo.githubusercontent.com/3f2acd1e98d3dee2feb0c2875f403a4f8f3ace63fb0be48055b726975abd3cad/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70617261676f6e69652f6561737964622e737667)](https://packagist.org/packages/paragonie/easydb)

PDO lacks brevity and simplicity; EasyDB makes separating data from instructions easy (and aesthetically pleasing).

EasyDB was created by [Paragon Initiative Enterprises](https://paragonie.com)as part of our effort to encourage better [application security](https://paragonie.com/service/appsec) practices.

Check out our other [open source projects](https://paragonie.com/projects) too.

Installing EasyDB
-----------------

[](#installing-easydb)

First, [get Composer](https://getcomposer.org/download/), if you don't already use it.

Next, run the following command:

```
/path/to/your/local/composer.phar require paragonie/easydb:^2
```

If you've installed Composer in `/usr/bin`, you can replace `/path/to/your/local/composer.phar` with just `composer`.

Why Use EasyDB? Because it's cleaner!
-------------------------------------

[](#why-use-easydb-because-its-cleaner)

Let's refactor a dangerous PHP snippet that previously used string concatenation to pass user input instead of prepared statements. For example, imagine something that just dropped `{$_GET['blogpostid']}` into the middle of a `mysql_query()` statement. Let's make it secure.

### The PDO Way

[](#the-pdo-way)

```
$db = new \PDO(
    'mysql:host=localhost;dbname=something',
    'username',
    'putastrongpasswordhere'
);

$statement = $db->prepare('SELECT * FROM comments WHERE blogpostid = ? ORDER BY created ASC');
$exec = $statement->execute([$_GET['blogpostid']]);
$rows = $exec->fetchAll(\PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $template_engine->render('comment', $row);
}
```

That's a little wordy for such a simple task. If we do this in multiple places, we end up repeating ourselves a lot.

### The EasyDB Solution

[](#the-easydb-solution)

```
$db = \EasyDB\Factory::create(
    'mysql:host=localhost;dbname=something',
    'username',
    'putastrongpasswordhere'
);

$rows = $db->run('SELECT * FROM comments WHERE blogpostid = ? ORDER BY created ASC', $_GET['blogpostid']);
foreach ($rows as $row) {
    $template_engine->render('comment', $row);
}
```

We made it a one-liner.

What else can EasyDB do quickly?
--------------------------------

[](#what-else-can-easydb-do-quickly)

### Insert a row into a database table

[](#insert-a-row-into-a-database-table)

```
$db->insert('comments', [
    'blogpostid' => $_POST['blogpost'],
    'userid' => $_SESSION['user'],
    'comment' => $_POST['body'],
    'parent' => isset($_POST['replyTo']) ? $_POST['replyTo'] : null
]);
```

#### Build an insert without executing

[](#build-an-insert-without-executing)

```
$sql = $db->buildInsertQuery('comments', [
    'blogpostid',
    'userid',
    'comment'
]);

// INSERT INTO comments (blogpostid, userid, comment) VALUES (?, ?, ?)

$result = $db->q(
    $sql,
    $values,
    \PDO::FETCH_BOTH,
    true
);
```

### Update a row from a database table

[](#update-a-row-from-a-database-table)

```
$db->update('comments', [
    'approved' => true
], [
    'commentid' => $_POST['comment']
]);
```

### Delete a row from a database table

[](#delete-a-row-from-a-database-table)

```
// Delete all of this user's comments
$db->delete('comments', [
    'userid' => 3
]);
```

### Fetch a single row from a table

[](#fetch-a-single-row-from-a-table)

```
$userData = $db->row(
    "SELECT * FROM users WHERE userid = ?",
    $_GET['userid']
);
```

### Fetch a single column from a single row from a table

[](#fetch-a-single-column-from-a-single-row-from-a-table)

```
$exists = $db->cell(
    "SELECT count(id) FROM users WHERE email = ?",
    $_POST['email']
);
/* OR YOU CAN CALL IT THIS WAY: */
$exists = $db->single(
    "SELECT count(id) FROM users WHERE email = ?",
    array(
        $_POST['email']
    )
);
```

### Try to perform a transaction

[](#try-to-perform-a-transaction)

```
$save = function (EasyDB $db) use ($userData, $query) {
    $db->safeQuery($query, [$userData['userId']]);
    \Some\Other\Package::CleanUpTable($db);
};
// auto starts, commits and rolls back a transaction as necessary
$db->tryFlatTransaction($save);
```

### Generate dynamic query conditions

[](#generate-dynamic-query-conditions)

```
$statement = EasyStatement::open()
    ->with('last_login IS NOT NULL');

if (strpos($_POST['search'], '@') !== false) {
    // Perform a username search
    $statement->orWith('username LIKE ?', '%' . $db->escapeLikeValue($_POST['search']) . '%');
} else {
    // Perform an email search
    $statement->orWith('email = ?', $_POST['search']);
}

// The statement can compile itself to a string with placeholders:
echo $statement; /* last_login IS NOT NULL OR username LIKE ? */

// All the values passed to the statement are captured and can be used for querying:
$user = $db->single("SELECT * FROM users WHERE $statement", $statement->values());
```

***Note**: Passing values with conditions is entirely optional but recommended.*

#### Variable number of "IN" arguments

[](#variable-number-of-in-arguments)

```
// Statements also handle translation for IN conditions with variable arguments,
// using a special ?* placeholder:
$roles = [1];
if ($_GET['with_managers']) {
    $roles[] = 2;
}

$statement = EasyStatement::open()->in('role IN (?*)', $roles);

// The ?* placeholder is replaced by the correct number of ? placeholders:
echo $statement; /* role IN (?, ?) */

// And the values will be unpacked accordingly:
print_r($statement->values()); /* [1, 2] */
```

#### Grouping of conditions

[](#grouping-of-conditions)

```
// Statements can also be grouped when necessary:
$statement = EasyStatement::open()
    ->group()
        ->with('subtotal > ?')
        ->andWith('taxes > ?')
    ->end()
    ->orGroup()
        ->with('cost > ?')
        ->andWith('cancelled = 1')
    ->end();

echo $statement; /* (subtotal > ? AND taxes > ?) OR (cost > ? AND cancelled = 1) */
```

What if I need PDO for something specific?
------------------------------------------

[](#what-if-i-need-pdo-for-something-specific)

```
$pdo = $db->getPdo();
```

Can I create an EasyDB wrapper for an existing PDO instance?
------------------------------------------------------------

[](#can-i-create-an-easydb-wrapper-for-an-existing-pdo-instance)

**Yes!** It's as simple as doing this:

```
$easy = new \EasyDB\EasyDB($pdo, 'mysql');
```

How do I run tests ?
--------------------

[](#how-do-i-run-tests-)

```
vendor/bin/phpunit
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Total

2

Last Release

3089d ago

### Community

Maintainers

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

---

Top Contributors

[![paragonie-scott](https://avatars.githubusercontent.com/u/11591518?v=4)](https://github.com/paragonie-scott "paragonie-scott (94 commits)")[![SignpostMarv](https://avatars.githubusercontent.com/u/304403?v=4)](https://github.com/SignpostMarv "SignpostMarv (85 commits)")[![shadowhand](https://avatars.githubusercontent.com/u/38203?v=4)](https://github.com/shadowhand "shadowhand (27 commits)")[![nfreader](https://avatars.githubusercontent.com/u/884548?v=4)](https://github.com/nfreader "nfreader (3 commits)")[![mignaulo](https://avatars.githubusercontent.com/u/1916624?v=4)](https://github.com/mignaulo "mignaulo (1 commits)")[![colshrapnel](https://avatars.githubusercontent.com/u/2895470?v=4)](https://github.com/colshrapnel "colshrapnel (1 commits)")[![LeSuisse](https://avatars.githubusercontent.com/u/737767?v=4)](https://github.com/LeSuisse "LeSuisse (1 commits)")[![paragonie-security](https://avatars.githubusercontent.com/u/15914520?v=4)](https://github.com/paragonie-security "paragonie-security (1 commits)")[![gavinggordon](https://avatars.githubusercontent.com/u/7691513?v=4)](https://github.com/gavinggordon "gavinggordon (1 commits)")[![edueo](https://avatars.githubusercontent.com/u/6653555?v=4)](https://github.com/edueo "edueo (1 commits)")

---

Tags

securitydatabasesqlpdo

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sw897-easydb/health.svg)

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

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

Easy-to-use database abstraction

744273.4k23](/packages/paragonie-easydb)[faapz/pdo

Just another PDO database library

314135.1k4](/packages/faapz-pdo)[paragonie/easydb-cache

Caching Adapter for EasyDB (caches Prepared Statements to reduce round trips)

3120.3k2](/packages/paragonie-easydb-cache)[delight-im/db

Safe and convenient SQL database access in a driver-agnostic way

49156.8k7](/packages/delight-im-db)

PHPackages © 2026

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