PHPackages                             phpgt/database - 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. phpgt/database

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

phpgt/database
==============

Database API organisation.

v1.6.3(7mo ago)49.7k↓30%3[9 issues](https://github.com/PhpGt/Database/issues)[3 PRs](https://github.com/PhpGt/Database/pulls)3MITPHPPHP &gt;=8.1CI passing

Since Nov 10Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/PhpGt/Database)[ Packagist](https://packagist.org/packages/phpgt/database)[ GitHub Sponsors](https://github.com/sponsors/PhpGt)[ RSS](/packages/phpgt-database/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (48)Used By (3)

[![Database logic organisation.](logo.png)](logo.png)

Database API organisation.
==========================

[](#database-api-organisation)

Encloses your application's database scripts within a simple and standardised interface, separating database access from your application logic.

The first parameter to any database functions is always the query name, which represents a query file on disk - either a raw SQL file or a PHP representation of a query using [SqlBuilder](https://www.php.gt/sqlbuilder).

---

[ ![Build status](https://camo.githubusercontent.com/0c33169f4a33eb2fc7e1b43dac0c3cdb765d1929cb75165737f3a9ecc7fb7966/68747470733a2f2f62616467652e7374617475732e7068702e67742f64617461626173652d6275696c642e737667)](https://github.com/PhpGt/Database/actions)[ ![Code quality](https://camo.githubusercontent.com/ad6ef386c0058e19e1499deb829c95c6f20a5888bb7ef732d04ef24cb93d0432/68747470733a2f2f62616467652e7374617475732e7068702e67742f64617461626173652d7175616c6974792e737667)](https://app.codacy.com/gh/PhpGt/Database)[ ![Code coverage](https://camo.githubusercontent.com/e2c5d4dbb6839b261823e6958cc1bbc69380875bac3067b8a3970c8b3b8ae90c/68747470733a2f2f62616467652e7374617475732e7068702e67742f64617461626173652d636f7665726167652e737667)](https://app.codecov.io/gh/PhpGt/Database)[ ![Current version](https://camo.githubusercontent.com/cc9e4a3e03204509aa8bc640b160b12e8bae7aaa6d91a594db5ea9f4850aa5d7/68747470733a2f2f62616467652e7374617475732e7068702e67742f64617461626173652d76657273696f6e2e737667)](https://packagist.org/packages/PhpGt/Database)[ ![PHP.Gt/Database documentation](https://camo.githubusercontent.com/ed7cd0c0d735d1f8f7402cdc9f10283dfff124f1ffffb492ccbbacf8a2be31d6/68747470733a2f2f62616467652e7374617475732e7068702e67742f64617461626173652d646f63732e737667)](http://www.php.gt/database)Example usage
-------------

[](#example-usage)

This library organises SQL access through a consistent API. To execute an example query located at `src/query/user/getById.sql`, the following pattern is used:

```
$userRow = $db->fetch("user/getById", 105);
```

Examples of CRUD operations:

```
// "fetchAll" method returns an iterable ResultSet of Row objects.
$bookResultSet = $db->fetchAll("shopitem/getItemsInCategory", "books");

foreach($bookResultSet as $bookRow) {
	echo "Book title: ", $bookRow->getString("title"), PHP_EOL;
	echo "Book price: £", ($bookRow->getFloat("price") + $bookRow->getFloat("vat")), PHP_EOL;

	if($bookRow->offerEnds) {
		echo "Item on offer until: ", $bookRow->getDateTime("offerEnds")->format("dS M Y");
	}
}

// "Create" method always returns the last inserted ID:
$newCustomerId = $db->create("customer/new", [
	"first_name" => "Marissa",
	"last_name" => "Mayer",
	"dob" => new DateTime("1975-05-30"),
]);

// "Update" or "delete" methods always return the number of affected rows:
$numberOfItemsAffected = $db->update("shop/item/increasePrice", [
	"percent" => 12.5,
	"max_increase" => 20.00,
]);

$numberOfDeletedReviews = $db->delete(
	"remove/deleteOlderThan",
	new DateTime("-6 months")
);

// Individual type-safe fields can be pulled from queries that return only one column:
$userName = $db->fetchString("user/getUsernameById", 105);
```

Features at a glance
--------------------

[](#features-at-a-glance)

- [Automatic database migrations](https://www.php.gt/docs/database/migrations)
- [Encapsulation of queries using `QueryCollection`s](https://www.php.gt/docs/database/query-collections)
- [Bind parameters by name or sequentially](https://www.php.gt/docs/database/parameters)
- [Fully configurable](https://www.php.gt/docs/database/config)
- [Type safe getters](https://www.php.gt/docs/database/type-safety)

Proudly sponsored by
====================

[](#proudly-sponsored-by)

[JetBrains Open Source sponsorship program](https://www.jetbrains.com/community/opensource/)

[![JetBrains logo.](https://camo.githubusercontent.com/b5639e7738c6dfae9fe3f3e20175570b7376ce2577a772e09c25c2d4f14bf86e/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f6a6574627261696e732e737667)](https://www.jetbrains.com/community/opensource/)

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance58

Moderate activity, may be stable

Popularity30

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity85

Battle-tested with a long release history

 Bus Factor3

3 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 ~88 days

Recently: every ~181 days

Total

38

Last Release

216d ago

Major Versions

v0.8.4 → v1.0.02019-02-08

PHP version history (5 changes)v0.6.0PHP ^7.1

v0.8.4PHP ^7.2

v1.1.4PHP &gt;=7.2

1.3.0PHP &gt;=7.4

v1.4.1PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/9e42344b91ce4b91ab57875969f67a0a6a48de570a08bc65d673b06b72fd3a3f?d=identicon)[g105b](/maintainers/g105b)

---

Top Contributors

[![g105b](https://avatars.githubusercontent.com/u/358014?v=4)](https://github.com/g105b "g105b (16 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (15 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (15 commits)")[![j4m3s](https://avatars.githubusercontent.com/u/1998995?v=4)](https://github.com/j4m3s "j4m3s (12 commits)")[![christopher-paul-shaw](https://avatars.githubusercontent.com/u/12035126?v=4)](https://github.com/christopher-paul-shaw "christopher-paul-shaw (4 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

databasedatabase-accessdatabase-api-organisationdatabase-logic-organisationdatabase-migrationsphp-libraryphp-pdophpgtsqlsql-databasesql-query

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpgt-database/health.svg)

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

###  Alternatives

[thecodingmachine/tdbm

The Database Machine is a PHP ORM that requires no configuration. The object model is deduced from the database model.

123174.9k6](/packages/thecodingmachine-tdbm)[rexshijaku/sql-to-laravel-builder

SQL to Laravel Query Builder

18925.9k1](/packages/rexshijaku-sql-to-laravel-builder)[circle/doctrine-rest-driver

Use a REST API as if it was your local database

15634.5k](/packages/circle-doctrine-rest-driver)[mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

22256.8k3](/packages/mouf-magic-query)

PHPackages © 2026

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