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

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

finesse/micro-db
================

A simple database connector for using pure men's SQL with bindings 💪

v0.2.3(8y ago)11.6k21MITPHPPHP &gt;=7.0CI failing

Since Oct 26Pushed 6y ago1 watchersCompare

[ Source](https://github.com/Finesse/MicroDB)[ Packagist](https://packagist.org/packages/finesse/micro-db)[ Docs](https://github.com/FinesseRus/MicroDB)[ RSS](/packages/finesse-micro-db/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (4)Dependencies (2)Versions (6)Used By (1)

MicroDB
=======

[](#microdb)

[![Latest Stable Version](https://camo.githubusercontent.com/98e4a1a8b4635b35ee50af7a88064737736ce92b0f568af5509724020383127e/68747470733a2f2f706f7365722e707567782e6f72672f66696e657373652f6d6963726f2d64622f762f737461626c65)](https://packagist.org/packages/finesse/micro-db)[![Total Downloads](https://camo.githubusercontent.com/d7b97f79d2297ab7b1ec32a77725b77730c90cb62759905f4b5954ef116fd456/68747470733a2f2f706f7365722e707567782e6f72672f66696e657373652f6d6963726f2d64622f646f776e6c6f616473)](https://packagist.org/packages/finesse/micro-db)[![PHP from Packagist](https://camo.githubusercontent.com/acaaa3a677201c08117f35f427dd1ffbac0579e8faa7be24ff34814e7b110521/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f66696e657373652f6d6963726f2d64622e737667)](https://camo.githubusercontent.com/acaaa3a677201c08117f35f427dd1ffbac0579e8faa7be24ff34814e7b110521/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f66696e657373652f6d6963726f2d64622e737667)[![Test Status](https://github.com/finesse/MicroDB/workflows/Test/badge.svg)](https://github.com/Finesse/MicroDB/actions?workflow=Test)[![Maintainability](https://camo.githubusercontent.com/7f9a44d5d74d952c8fa748f942e9f04ea5dadeb266933096b559ba23f14f5ebf/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f66346433626362643534633031326566346561662f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/Finesse/MicroDB/maintainability)[![Test Coverage](https://camo.githubusercontent.com/963b747dd8a021d37b6445588b4044c773f0e56abde61feff5b2e40502e00e57/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f66346433626362643534633031326566346561662f746573745f636f766572616765)](https://codeclimate.com/github/Finesse/MicroDB/test_coverage)

Like to use pure SQL but don't like to suffer from PDO, mysqli or etc.? Try this.

```
$database = Connection::create('mysql:host=localhost;dbname=my_database', 'user', 'pass');
$items = $database->select('SELECT * FROM items WHERE category_id = ?', [3]);
```

Key features:

- No silly query builder, only a good old SQL.
- Very light, no external dependencies. It required only the [PDO extension](http://php.net/manual/en/book.pdo.php) which is available by default in most of servers.
- Database object is delivered explicitly, not through a static class.
- Exceptions on errors.

You can combine it with a third-party SQL query builder to rock the database. Examples of suitable query builders: [Query Scribe](https://github.com/Finesse/QueryScribe), [Nilportugues SQL Query Builder](https://github.com/nilportugues/php-sql-query-builder), [Aura.SqlQuery](https://github.com/auraphp/Aura.SqlQuery), [Latitude](https://github.com/shadowhand/latitude), [Koine Query Builder](https://github.com/koinephp/QueryBuilder), [Phossa2 Query](https://github.com/phossa2/query), [Hydrahon](https://github.com/ClanCats/Hydrahon).

Installation
------------

[](#installation)

### Using [Composer](https://getcomposer.org)

[](#using-composer)

Run in a console

```
composer require finesse/micro-db
```

Reference
---------

[](#reference)

### Create a `Connection` instance

[](#create-a-connection-instance)

To create a new `Connection` instance call the `create` method passing [PDO constructor arguments](http://php.net/manual/en/pdo.construct.php).

```
use Finesse\MicroDB\Connection;

$database = Connection::create('dsn:string', 'username', 'password, ['options']);
```

Or pass a `PDO` instance to the constructor. But be careful: `Connection` *changes* the given `PDO` object and you *must not* change the given object, otherwise something unexpected will happen.

```
use Finesse\MicroDB\Connection;

$pdo = new PDO(/* ... */);
$database = new Connection($pdo);
```

### Select

[](#select)

Select many rows:

```
$rows = $database->select('SELECT * FROM table'); // [['id' => 1, 'name' => 'Bill'], ['id' => 2, 'name' => 'John']]
```

Select one row:

```
$row = $database->selectFirst('SELECT * FROM table'); // ['id' => 1, 'name' => 'Bill']
```

The cell values are returned as they are returned by PDO. They are not casted automatically because casting can cause data loss.

### Insert

[](#insert)

Insert and get the number of the inserted rows:

```
$insertedCount = $database->insert('INSERT INTO table (id, price) VALUES (1, 45), (2, 98)'); // 2
```

Insert and get the identifier of the last inserted row:

```
$id = $database->insertGetId('INSERT INTO table (weight, price) VALUES (12.3, 45)'); // 3
```

### Update

[](#update)

Update rows and get the number of the updated rows:

```
$updatedCount = $database->update('UPDATE table SET status = 1 WHERE price < 1000');
```

### Delete

[](#delete)

Delete rows and get the number of the deleted rows:

```
$deletedCount = $database->delete('DELETE FROM table WHERE price > 1000');
```

### Other queries

[](#other-queries)

Perform any other statement:

```
$database->statement('CREATE TABLE table(id INTEGER PRIMARY KEY ASC, name TEXT, price NUMERIC)');
```

If the query contains multiple statements separated by a semicolon, only the first statement will be executed. You can execute multiple statements using the other method:

```
$database->statements("
    CREATE TABLE table(id INTEGER PRIMARY KEY ASC, name TEXT, price NUMERIC);
    INSERT INTO table (name, price) VALUES ('Donald', 1000000);
");
```

The lack of this method is that it doesn't take values to bind.

### Execute a file

[](#execute-a-file)

Execute the query from an SQL file:

```
$database->import('path/to/file.sql');
```

Or from a resource:

```
$stream = fopen('path/to/file.sql', 'r');
$database->import($stream);
```

### Binding values

[](#binding-values)

You should not insert values right to an SQL query because it can cause [SQL injections](https://en.wikipedia.org/wiki/SQL_injection). Instead use the binding:

```
// WRONG! Don't do it or you will be fired
$rows = $database->select("SELECT * FROM table WHERE name = '$name' LIMIT $limit");

// Good
$rows = $database->select('SELECT * FROM table WHERE name = ? LIMIT ?', [$name, $limit]);
```

Database server replaces the placeholders (`?`s) safely with the given values. Almost all the above methods accepts the list of the bound values as the second argument.

You can also use named parameters:

```
$rows = $database->select('SELECT * FROM table WHERE name = :name LIMIT :limit', [':name' => $name, ':limit' => $limit]);
```

You can even pass named and anonymous parameters in the same array but it works only when the array of values has the same order as the placeholders in the query text.

All the scalar types of values are supported: string, integer, float, boolean and null.

### Error handling

[](#error-handling)

The `Finesse\MicroDB\Exceptions\PDOException` is thrown in case of every database query error. If an error is caused by an SQL query, the exception has the query text and bound values in the message. They are also available through the methods:

```
$sql = $exception->getQuery();
$bindings = $exception->getValues();
```

The `Finesse\MicroDB\Exceptions\InvalidArgumentException` is thrown when the method arguments have a wrong format.

The `Finesse\MicroDB\Exceptions\FileException` is thrown on a file read error.

All the exceptions implement `Finesse\MicroDB\IException`.

### Retrieve the underlying `PDO` object

[](#retrieve-the-underlying-pdo-object)

```
$pdo = $database->getPDO();
```

You *must not* change the retrieved object, otherwise something unexpected will happen.

Known problems
--------------

[](#known-problems)

- `insertGetId` doesn't return the inserted row identifier for SQL Server and PostgreSQL.
- `statements` and `import` don't throw an exception if the second or a next statement of the query has an error. This is [a PDO bug](https://stackoverflow.com/a/28867491/1118709).

Make a pull request or an issue if you need a problem to be fixed.

Versions compatibility
----------------------

[](#versions-compatibility)

The project follows the [Semantic Versioning](http://semver.org).

License
-------

[](#license)

MIT. See [the LICENSE](LICENSE) file for details.

###  Health Score

28

—

LowBetter than 51% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

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

Total

5

Last Release

3078d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9006227?v=4)[Sergey M.](/maintainers/Finesse)[@Finesse](https://github.com/Finesse)

---

Top Contributors

[![Finesse](https://avatars.githubusercontent.com/u/9006227?v=4)](https://github.com/Finesse "Finesse (44 commits)")

---

Tags

databaselibrarymysqlpdopure-sqlsqldatabasemysqlsqlpdo

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[doctrine/dbal

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

9.7k614.3M7.4k](/packages/doctrine-dbal)[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k6.2M84](/packages/ifsnop-mysqldump-php)[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k23.2k](/packages/clouddueling-mysqldump-php)[aura/sqlschema

Provides facilities to read table names and table columns from a database using PDO.

41245.4k4](/packages/aura-sqlschema)[delight-im/db

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

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

PHPackages © 2026

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