PHPackages                             ludal/mysql-querybuilder - 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. ludal/mysql-querybuilder

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

ludal/mysql-querybuilder
========================

A simple PHP query builder for SQL queries

v0.5.2(4y ago)157671[8 issues](https://github.com/iamludal/MySQL-QueryBuilder/issues)1MITPHPPHP &gt;=7.0CI failing

Since May 28Pushed 3y ago2 watchersCompare

[ Source](https://github.com/iamludal/MySQL-QueryBuilder)[ Packagist](https://packagist.org/packages/ludal/mysql-querybuilder)[ Docs](https://github.com/iamludal/PHP-QueryBuilder)[ RSS](/packages/ludal-mysql-querybuilder/feed)WikiDiscussions develop Synced 6d ago

READMEChangelogDependencies (1)Versions (14)Used By (1)

🔧 MySQL QueryBuilder
====================

[](#-mysql-querybuilder)

[![PHPUnit](https://github.com/iamludal/mysql-querybuilder/actions/workflows/run-tests.yml/badge.svg)](https://github.com/iamludal/mysql-querybuilder/actions/workflows/run-tests.yml)[![Version](https://camo.githubusercontent.com/8e73292fdf8d733504f253730bd665fcba3092710729a9fb4c0f59ca92b8ec0a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f69616d6c7564616c2f5048502d51756572794275696c6465723f6c6162656c3d76657273696f6e)](https://camo.githubusercontent.com/8e73292fdf8d733504f253730bd665fcba3092710729a9fb4c0f59ca92b8ec0a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f69616d6c7564616c2f5048502d51756572794275696c6465723f6c6162656c3d76657273696f6e)[![PHP Version](https://camo.githubusercontent.com/8b27d0d9917f2f9aad8d99ca15db7c13240c41c9d2856cadbf5db558d22799ee/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c7564616c2f6d7973716c2d71756572796275696c6465723f636f6c6f723d626c756576696f6c6574)](https://camo.githubusercontent.com/8b27d0d9917f2f9aad8d99ca15db7c13240c41c9d2856cadbf5db558d22799ee/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c7564616c2f6d7973716c2d71756572796275696c6465723f636f6c6f723d626c756576696f6c6574)[![License](https://camo.githubusercontent.com/d1be739576d301fc0f406cd009aae007967f9207ac8ba8f930af2fc61df48008/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c7564616c2f6d7973716c2d71756572796275696c6465723f636f6c6f723d6f72616e6765)](https://camo.githubusercontent.com/d1be739576d301fc0f406cd009aae007967f9207ac8ba8f930af2fc61df48008/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c7564616c2f6d7973716c2d71756572796275696c6465723f636f6c6f723d6f72616e6765)[![Codacy Badge](https://camo.githubusercontent.com/1f8426affae6435093722848421f1e7806a7b9cbec081ea9455de7d6e89af26e/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3861623830346536306333383434356138653138346332363463303663643435)](https://www.codacy.com/manual/iamludal/PHP-QueryBuilder?utm_source=github.com&utm_medium=referral&utm_content=iamludal/PHP-QueryBuilder&utm_campaign=Badge_Grade)

```
composer require ludal/mysql-querybuilder

```

ℹ️ Presentation
---------------

[](#ℹ️-presentation)

This is a PHP query builder for simple MySQL queries. It allows you to write them without using strings nor heredoc, which often breaks the code's cleanliness.

This package is designed to support MySQL DBMS. However, it may work with other DBMS (PostgreSQL, SQLite...), **but I cannot guarantee that, so use it at your own risk**.

> 💡 Made with ❤️ in 🇫🇷

📘 Usage
-------

[](#-usage)

### Getting Started

[](#getting-started)

First, initialize a new instance of `QueryBuilder`.

```
$builder = new QueryBuilder();
```

> 💡 You can also pass a PDO instance as a parameter to execute and fetch queries directly.
>
> ```
> $pdo = new PDO($dsn, $login, $password);
> $builder = new QueryBuilder($pdo);
> ```

From this instance, you can now build your query:

```
$select = $builder
  ->select()
  ->from('users')
  ->where('name = :name');

$update = $builder
  ->update('users')
  ->set(['name' => 'John'])
  ->where('id = 6');
```

Then, you can either:

- Convert your query into a SQL string : `toSQL()`
- Bind parameters: `setParam('name', 'John')`, `setParams(['name' => 'John'])`...
- Execute the query : `execute()`, `execute(['John'])`...
- Fetch the results of your query : `fetch()`, `fetchAll()`, `fetch(PDO::FETCH_COLUMN)`...
- Get the rowCount : `rowCount()`
- Get the PDO statement corresponding to your query : `getStatement()`
- And more: see docs for a full reference

```
$select->toSQL(); // returns 'SELECT * FROM users'

$select->fetchAll(); // returns the rows fetched from the db

$select->getStatement(); // get the PDO statement, useful for handling errors

$update->execute(); // executes the UPDATE query
```

### Supported Statements

[](#supported-statements)

- `SELECT`
- `UPDATE`
- `DELETE FROM`
- `INSERT INTO`

### Supported Clauses

[](#supported-clauses)

- `WHERE`
- `GROUP BY`
- `ORDER BY`
- `LIMIT`
- `OFFSET`

### Code Samples

[](#code-samples)

```
$pdo = new PDO(...);
$qb = new QueryBuilder($pdo);

QueryBuilder::setDefaultFetchMode(PDO::FETCH_ASSOC);

// SELECT
$res = $qb
  ->select()
  ->from('users')
  ->where('id < 4', 'name = :name')
  ->orWhere('age < 12')
  ->orderBy('id', 'desc')
  ->limit(2)
  ->offset(1)
  ->fetchAll();

// INSERT
$insert = $qb
  ->insertInto('articles')
  ->values(['title' => 'Lorem ipsum', 'content' => 'Some content'])
  ->getStatement();

$insert->execute();
$insert->errorCode(); // or any other PDOStatement method

// UPDATE
$updated = $qb
  ->update('connections')
  ->set(['exp' => true, 'date' => date('Y-m-d')])
  ->where(['token' => $token])
  ->orderBy('date')
  ->limit(1)
  ->execute();

// DELETE
$rowCount = $qb
  ->deleteFrom('users')
  ->where('id > 5')
  ->orWhere('name = :name')
  ->orderBy('id', 'desc')
  ->limit(10)
  ->setParam(':name', 'John')
  ->rowCount(); // will execute, and return the rowCount
```

📖 Docs
------

[](#-docs)

[Wiki](https://github.com/iamludal/PHP-QueryBuilder/wiki) under construction. 🚧

🙏 Acknowledgements
------------------

[](#-acknowledgements)

- [Vincent Aranega](https://github.com/aranega) for tips and tricks about the code organization

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance9

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.6% 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 ~51 days

Recently: every ~85 days

Total

11

Last Release

1664d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/46377b30f36af4191688495b58655711df7dd3704176b012540d71a2e6c11e9d?d=identicon)[iamludal](/maintainers/iamludal)

---

Top Contributors

[![iamludal](https://avatars.githubusercontent.com/u/44783088?v=4)](https://github.com/iamludal "iamludal (146 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![nimah79](https://avatars.githubusercontent.com/u/20343056?v=4)](https://github.com/nimah79 "nimah79 (1 commits)")

---

Tags

hacktoberfestmysqlmysql-query-builderpdophpphp-query-builderphp-querybuilderquerybuildersqlquerybuilderquerybuilderquery builder

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ludal-mysql-querybuilder/health.svg)

```
[![Health](https://phpackages.com/badges/ludal-mysql-querybuilder/health.svg)](https://phpackages.com/packages/ludal-mysql-querybuilder)
```

###  Alternatives

[nilportugues/sql-query-builder

An elegant lightweight and efficient SQL QueryInterface BuilderInterface supporting bindings and complicated query generation.

425239.4k6](/packages/nilportugues-sql-query-builder)[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[izniburak/pdox

Useful Query Builder, PDO Class for PHP. A simple access to your SQL records.

30221.1k7](/packages/izniburak-pdox)[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)[sad_spirit/pg_builder

Query builder for Postgres backed by SQL parser

5933.6k1](/packages/sad-spirit-pg-builder)

PHPackages © 2026

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