PHPackages                             xrstf/comfydb - 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. xrstf/comfydb

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

xrstf/comfydb
=============

ComfyDB is a tiny wrapper around mysqli to provide some convenience functions and sprintf-like query construction.

0.2.1(9y ago)012MITPHPPHP &gt;=5.4.0

Since Jan 30Pushed 9y ago1 watchersCompare

[ Source](https://github.com/xrstf/comfydb)[ Packagist](https://packagist.org/packages/xrstf/comfydb)[ RSS](/packages/xrstf-comfydb/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (5)Used By (0)

ComfyDB
=======

[](#comfydb)

ComfyDB is a wrapper around PHP's mysqli extension and aimed to provide convenience methods for common tasks like fetching one row, fetching one column etc.

Note that performance is not the main focus of this library. All fetched result sets will be read in full, so when you want to stream-process large result sets, this might not be the droid you're looking for.

Also, this library is not using mysqli's prepared statement support to handle parameterized queries. Instead, a sprintf-like approach is used, where placeholders like `%s` or `%d` are replaced before the query is sent to to the server.

Examples: Connecting
--------------------

[](#examples-connecting)

Connect to a database:

```
use xrstf\ComfyDB;

$db = ComfyDB::connect('host', 'user', 'password', 'database');

// alternatively, wrap an existing mysqli connection
$db = new ComfyDB($mysqliConnection);
```

Examples: Query Data
--------------------

[](#examples-query-data)

Select data. Rows are always associative arrays. No magic here.

```
$rows = $db->query('SELECT id, firstname, lastname FROM persons');

/*
$rows = [
    ['id' => 1, 'firstname' => 'Tom', 'lastname', 'Schlock'],
    ['id' => 2, 'firstname' => 'Max', 'lastname', 'Power'],
    ['id' => 3, 'firstname' => 'Maria', 'lastname', 'Gomez'],
    ...
];
*/

// use %s for strings, %d for integers, %f for floats and %n for NULLable values
// (%n will result in 'NULL' if the given value is null, otherwise it will encode
// the value as a string, like %s)
$rows = $db->query('SELECT * FROM persons WHERE firstname = %s AND id = %d', ['Tom', 1]);
```

Fetch a single column from the result set. The return value is a flat array of the values.

```
$names = $db->fetchColumn('SELECT firstname FROM persons WHERE 1');
// $names = ['Max', 'Tom', 'Maria'];
```

Fetch a result set and use the first column as the key in the final result. If only two columns are selected, the value in the final map is not an array, but the second column.

```
// select three columns, use ID as the key
$names = $db->fetchMap('SELECT id, firstname, lastname FROM persons WHERE 1');

/*
$names = [
    1 => ['firstname' => 'Tom', 'lastname', 'Schlock'],
    2 => ['firstname' => 'Max', 'lastname', 'Power'],
    3 => ['firstname' => 'Maria', 'lastname', 'Gomez'],
];
*/

// select two columns
$names = $db->fetchMap('SELECT id, firstname FROM persons WHERE 1');

/*
$names = [
    1 => 'Tom',
    2 => 'Max',
    3 => 'Maria',
];
*/
```

Fetch a single row. If only one column is selected, that value of the first row is returned instead of an associative array. If no rows are found, `null` is returned. If more than one row is found, only the first is taken into consideration.

```
// fetch a single cell from the database
$firstname = $db->fetch('SELECT firstname FROM persons WHERE id = %d', [1]);
// $firstname = 'Tom'

// fetch more than one cell
$name = $db->fetch('SELECT firstname, lastname FROM persons WHERE id = %d', [1]);
// $name = ['firstname' => 'Tom', 'lastname', 'Schlock']

// find no rows (returns always null, disregarding of the number of columns selected)
$row = $db->fetch('SELECT * FROM table WHERE 1 = 2');
// $row = null
```

Examples: Update Data
---------------------

[](#examples-update-data)

`update()` takes the table name, the new data and the WHERE criteria.

```
$newData = [
    'firstname' => 'Anja',
    'lastname' => 'Muster',
];

// for simple conditions, you can give the WHERE criteria as an array
$db->update('persons', $newData, ['id' => 3]);

// more complex criteria can be given as a string, which is copied verbatim
$db->update('persons', $newData, '(firstname NOT NULL OR lastname NOT LIKE "%foo")');
```

Examples: Insert Data
---------------------

[](#examples-insert-data)

`insert()` takes the table name and the new data.

```
$newData = [
    'firstname' => 'Anja',
    'lastname' => 'Muster',
];

$db->insert('persons', $newData);

$id = $db->getInsertedID();
```

Examples: Delete Data
---------------------

[](#examples-delete-data)

`delete()` takes the table name and the WHERE criteria, like `update()`.

```
$db->delete('persons', ['id' => 2]);
$deleted = $db->getAffectedRows();
```

Examples: Error Handling
------------------------

[](#examples-error-handling)

In case of any error, a `xrstf\ComfyException` is thrown, which contains the error code, error message and the failed query.

```
try {
    $db->delete('nope', ['id' => 2]);
}
catch (ComfyException $e) {
    print "Query: ".$e->getQuery()."\n";
    print "Code : ".$e->getCode()."\n";
    print "Error: ".$e->getErrorMessage()."\n";
}
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

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

Total

4

Last Release

3619d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7f747ed6a51a81fbf9b8c2e36d44b979ad9d3018e8de079b3188a50d7878f3b0?d=identicon)[xrstf](/maintainers/xrstf)

---

Top Contributors

[![xrstf](https://avatars.githubusercontent.com/u/127499?v=4)](https://github.com/xrstf "xrstf (8 commits)")

---

Tags

databasepdo

### Embed Badge

![Health badge](/badges/xrstf-comfydb/health.svg)

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

###  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)
