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

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

ifcanduela/db
=============

1.6.0(3y ago)0376↓90%1MITPHPPHP &gt;=8.1

Since Aug 8Pushed 3y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (15)Used By (1)

db: Query Builder and Connection manager
========================================

[](#db-query-builder-and-connection-manager)

An easy-to-use database connection manager and query builder for SQLite and MySQL.

Getting started
---------------

[](#getting-started)

Install using [Composer](https://getcomposer.org).

Connecting to a database
------------------------

[](#connecting-to-a-database)

The `ifcanduela\db\Database` class extends PDO, but includes two static methods to connect to MySQL and SQLite:

```
require __DIR__ . '/vendor/autoload.php';

use ifcanduela\db\Database;

$sqlite = Database::sqlite($filename, $options);
$mysql  = Database::mysql($host, $dbname, $user, $password, $options);
```

The arguments match those in the [PDO constructor](http://php.net/manual/en/pdo.construct.php).

The following options are set by default when using the static factories to create a connection:

- PDO will throw exceptions on error.
- Results will be returned as associative arrays.
- Prepared statements will **not** be emulated.

### Create a connection using an array

[](#create-a-connection-using-an-array)

Connections can also be created using an array:

```
$mysql = Database::fromArray([
        'engine' => 'mysql',
        'host' => '127.0.0.1',
        'name' => 'some_database',
        'user' => 'some_username',
        'pass' => 'some_password',
    ]);

$sqlite = Database::fromArray([
        'engine' => 'sqlite',
        'file' => './db.sqlite',
    ]);
```

Query builder
-------------

[](#query-builder)

```
require __DIR__ . '/vendor/autoload.php';

use ifcanduela\db\Query;

$query = Query::select()
    ->columns('users.*')
    ->from('users')
    ->leftJoin('profiles', ['users.id' => 'profiles.user_id'])
    ->where(['status' => ['', 1]])
    ->orderBy('created DESC', 'username')
    ->limit(1, 2);

echo $query; // or $query->getSql();
// SELECT users.*
// FROM users LEFT JOIN profiles ON users.id = profiles.user_id
// WHERE status  :_param_1
// ORDER BY created DESC, username
// LIMIT 2, 1;
```

You can get the parameters for the prepared statement by calling `getParams()` on the `$query`object.

Running queries
---------------

[](#running-queries)

When you have a connection and have built a query, you can call the `run` method on the connection to run a query:

```
$sqlite->run($query);
```

Which is equivalent to this:

```
$sqlite->query($query->getSql(), $query->getParams());
```

Logging queries
---------------

[](#logging-queries)

Queries run through the run() method can be logged using an object implementing `LoggerInterface`. The query log entries use the `Logger::INFO` level. For example, using Monolog:

```
use ifcanduela\db\Database;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('Query log');
$file_handler = new StreamHandler('queries.log', Logger::INFO);
$logger->pushHandler($file_handler);

$db = Database::sqlite(':memory');
$db->setLogger($logger);

$db->run('SELECT 1');
```

Query builder API
-----------------

[](#query-builder-api)

### Select queries

[](#select-queries)

```
Query::select(string ...$field)
    ->distinct(bool $enable = true)
    ->columns(string ...$column)
    ->from(string ...$table)
    ->join(string $table, array $on)
    ->innerJoin(string $table, array $on)
    ->leftJoin(string $table, array $on)
    ->leftOuterJoin(string $table, array $on)
    ->rightJoin(string $table, array $on)
    ->outerJoin(string $table, array $on)
    ->fullOuterJoin(string $table, array $on)
    ->where(array $conditions)
    ->andWhere(array $conditions)
    ->orWhere(array $conditions)
    ->groupBy(string ...$field)
    ->having(array $conditions)
    ->andHaving(array $conditions)
    ->orHaving(array $conditions)
    ->orderBy(string ...$field)
    ->limit(int $limit, int $offset = null)
    ->offset(int $offset)
    ->getSql()
    ->getParams()
```

There is also a `Query::count()` method that will select a `COUNT(*)` column automatically.

### Insert queries

[](#insert-queries)

```
Query::insert(string $table = null)
    ->table(string $table)
    ->into(string $table)
    ->values(array ...$values)
    ->getSql()
    ->getParams()
```

### Update queries

[](#update-queries)

```
Query::update(string $table = null)
    ->table(string $table)
    ->set(array $values)
    ->where(array $conditions)
    ->andWhere(array $conditions)
    ->orWhere(array $conditions)
    ->getSql()
    ->getParams()
```

### Delete queries

[](#delete-queries)

```
Query::delete(string $table = null)
    ->table(string $table)
    ->where(array $conditions)
    ->andWhere(array $conditions)
    ->orWhere(array $conditions)
    ->getSql()
    ->getParams()
```

### Specifying conditions

[](#specifying-conditions)

Building conditions is accomplished by using the `where()`, `andWhere()` and `orWhere()`methods (or their grouping equivalents, `having()`, `andHaving()` and `orHaving()`). Conditions must be associative arrays, where keys are expected to be the column names in the comparison and the left-side value are values or indexed arrays of operator and value.

Values will be converted to prepared statement parameters unless you use the `ifcanduela\db\qi()` function on them.

An example of a select query with multiple conditions would be this:

```
$q = Query::select();

$q->columns('id', 'name', 'age');
$q->from('users');
$q->where(['id' => 1]);
$q->orWhere(['id' => 3]);
$q->andWhere(['age' => ['>', 18]]);
$q->orderBy('age DESC');
```

The resulting SQL will be similar to the following snippet:

```
SELECT id, name, age
FROM users
WHERE (id = :p_1 OR id = :p_2) AND age > :p_3
ORDER BY age DESC
```

And the parameters array would look like this:

```
[
    ":p_1" => 1,
    ":p_2" => 3,
    ":p_3" => 18,
]
```

#### Complex conditions

[](#complex-conditions)

If using the `where()` methods is confusing or insufficient, you can use simple arrays to specify nested conditions:

```
$q = Query::select()->where([
        'AND',
        'a' => 1,
        'b' => 2,
        [
            'OR',
            'c' => 3,
            'd' => 4,
        ]
    ]);
```

Which will result in something like this:

```
SELECT *
FROM users
WHERE a = :p_1 AND b = :p_2 AND (c = :p_3 OR d = :p_4)
```

License
-------

[](#license)

[MIT](LICENSE).

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity79

Established project with proven stability

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

Recently: every ~206 days

Total

14

Last Release

1287d ago

Major Versions

0.5 → 1.0.02018-02-25

PHP version history (3 changes)0.5PHP &gt;=7.0

1.4PHP &gt;=7.3

1.6.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1359230?v=4)[Igor Fernández Canduela](/maintainers/ifcanduela)[@ifcanduela](https://github.com/ifcanduela)

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[yemenopensource/filament-excel

This package useful for importing excel files into models.

194.2k](/packages/yemenopensource-filament-excel)

PHPackages © 2026

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