PHPackages                             girgias/query-builder - 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. girgias/query-builder

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

girgias/query-builder
=====================

A library to build valid SQL queries

0.4.0(7y ago)14141MITPHPPHP ^7.3

Since Dec 17Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Girgias/query-builder)[ Packagist](https://packagist.org/packages/girgias/query-builder)[ RSS](/packages/girgias-query-builder/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (6)Versions (5)Used By (0)

SQL Query Builder
=================

[](#sql-query-builder)

[![Build Status](https://camo.githubusercontent.com/ef7877675979529bc0fbf47bb93bd2d389535c15c0a52a76342f278c9a39de24/68747470733a2f2f7472617669732d63692e6f72672f476972676961732f71756572792d6275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Girgias/query-builder)[![Maintainability](https://camo.githubusercontent.com/315b27a2d9bfe6c15489f9fceb152750ecd131cefe98e73327cfec8ecc2ff140/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f65383034343836623638646634303830636561642f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/Girgias/query-builder/maintainability)[![Test Coverage](https://camo.githubusercontent.com/bcff7cc5c9fce5156d69da054c73a9901f6f247e0e83d86be03f3a7ff04aede8/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f65383034343836623638646634303830636561642f746573745f636f766572616765)](https://codeclimate.com/github/Girgias/query-builder/test_coverage)

A fluent SQL Query Builder which **ONLY** builds a valid SQL query with the SQL clauses it has been asked to provide.

Installing
----------

[](#installing)

```
composer require girgias/query-builder
```

Features
--------

[](#features)

This Query Builder can build a variety of SQL queries which are database agnostic as it uses the ANSI standardized syntax.

Every sort of query has its own class which extends from the base `Query` class, They all have the same constructor signature which requires the table name on which to execute the query.

To build a SELECT query with table join use the `SelectJoin` class. An example can be seen below on how to use this class.

It is possible to directly provide scalar values to WHERE clauses and while binding a field. It is also possible to specify the named parameter against which the value will be bounded. In case no named parameter has been provided a random one will be generated.

To retrieve the named parameters with their associated values use the `getParameters`method which will return an associative array `parameter => value`.

### Examples

[](#examples)

A basic `SELECT` query:

```
$query = (new \Girgias\QueryBuilder\Select('demo'))
    ->limit(10, 20)
    ->order('published_date')
    ->getQuery();
```

Will output:

```
SELECT * FROM demo ORDER BY published_date ASC LIMIT 10 OFFSET 20
```

A more complex `SELECT` query:

```
$start = new \DateTime('01/01/2016');
$end = new \DateTime('01/01/2017');
$query = (new \Girgias\QueryBuilder\Select('demo'))
    ->select('title', 'slug')
    ->selectAs('name_author_post', 'author')
    ->whereBetween('date_published', $start, $end)
    ->order('date_published', 'DESC')
    ->limit(25)
    ->getQuery();
```

Will output:

```
SELECT title, slug, name_author_post AS author FROM demo WHERE date_published BETWEEN '2016-01-01 00:00:00' AND '2017-01-01 00:00:00' ORDER BY date_published DESC LIMIT 25
```

An example with the `whereOr` method:

```
$query = (new \Girgias\QueryBuilder\Select('demo'))
    ->where('author', '=', 'Alice', 'author')
    ->whereOr('editor', '=', 'Alice', 'editor')
    ->getQuery();
```

Will output:

```
SELECT * FROM demo WHERE (author = :author OR editor = :editor)
```

`UPDATE` query example:

```
$query = (new \Girgias\QueryBuilder\Update('posts'))
    ->where('id', '=', 1, 'id')
    ->bindField('title', 'This is a title', 'title')
    ->bindField('content', 'Hello World', 'content')
    ->bindField('date_last_edited', (new \DateTimeImmutable()), 'nowDate')
    ->getQuery();
```

Will output:

```
UPDATE posts SET title = :title, content = :content, date_last_edited = :now_date WHERE id = :id
```

A `SELECT` query with an `INNER JOIN`:

```
$query = (new \Girgias\QueryBuilder\SelectJoin('comments', 'posts'))
    ->tableAlias('co')
    ->select('co.user', 'co.content', 'p.title')
    ->joinTableAlias('p')
    ->innerJoin('post_id', 'id')
    ->getQuery();
```

Will output:

```
SELECT co.user, co.content, p.title FROM comments AS co INNER JOIN posts AS p ON comments.post_id = posts.id
```

Future scope
------------

[](#future-scope)

Possible features that will be added to this library

- WHERE subqueries

Contributing
------------

[](#contributing)

If you found an invalid SQL name which **DOESN'T** throw a Runtime exception or a valid SQL name which does please add a test case into the `tests/CheckSqlNamesTest.php` file.

If you found an example where this library returns an invalid SQL query please add (or fix) a test case in the relevant Query test case or if it's a general error please use the `tests/QueryTest` file.

If a RunTime exception should be thrown please add a test in the relevant test file or if it is specific to `SELECT` Query please add a test in the `tests/SelectThrowExceptionsTest.php` file.

If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.

### Notes

[](#notes)

When contributing please assure that Psalm runs without error and all unit tests pass. Moreover if you add functionality please add corresponding unit tests to cover at least 90% of your code and that these tests cover any edge cases if they exist.

Links
-----

[](#links)

- Repository:
- Issue tracker:
    - In case of sensitive bugs like security vulnerabilities, please contact  directly instead of using the issue tracker.

Licensing
---------

[](#licensing)

The code in this project is licensed under MIT license.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity49

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

Total

4

Last Release

2649d ago

PHP version history (2 changes)0.1.0PHP ^7.1

0.3.0PHP ^7.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7906688?v=4)[Gina Peter Banyard](/maintainers/Girgias)[@Girgias](https://github.com/Girgias)

---

Top Contributors

[![Girgias](https://avatars.githubusercontent.com/u/7906688?v=4)](https://github.com/Girgias "Girgias (3 commits)")

---

Tags

databasephpquery-buildersqldatabasesqlquery builder

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/girgias-query-builder/health.svg)

```
[![Health](https://phpackages.com/badges/girgias-query-builder/health.svg)](https://phpackages.com/packages/girgias-query-builder)
```

###  Alternatives

[usmanhalalit/pixie

A lightweight, expressive, framework agnostic query builder for PHP.

6872.2M15](/packages/usmanhalalit-pixie)[foolz/sphinxql-query-builder

A PHP query builder for SphinxQL and ManticoreQL with MySQLi and PDO drivers.

3232.2M32](/packages/foolz-sphinxql-query-builder)[cycle/database

DBAL, schema introspection, migration and pagination

64690.9k31](/packages/cycle-database)[opis/database

A database abstraction layer over PDO, that provides a powerful and intuitive query builder, bundled with an easy to use schema builder

10184.2k3](/packages/opis-database)[jasny/persist-sql-query

SQL Query builder and parser

33486.0k4](/packages/jasny-persist-sql-query)[mrjgreen/database

Expressive Database Layer for PHP - Based on Illuminate/Database

5347.8k10](/packages/mrjgreen-database)

PHPackages © 2026

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