PHPackages                             ucscode/squery - 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. ucscode/squery

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

ucscode/squery
==============

SQL Syntax Generator for PHP

3.1.0(2y ago)0211MITPHPPHP &gt;=8.1

Since Jan 2Pushed 2y ago1 watchersCompare

[ Source](https://github.com/ucscode/SQuery)[ Packagist](https://packagist.org/packages/ucscode/squery)[ RSS](/packages/ucscode-squery/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (1)

SQuery
======

[](#squery)

SQL Syntax Generator for PHP

Introduction
============

[](#introduction)

Orignally made for [User Synthetics](https://github.com/ucscode/user-synthetics), SQuery has progressed to the point of becoming an independent PHP library that simplifies the process of generating SQL syntax for your database queries. With sQuery, you can build SQL queries using an intuitive and fluent interface, reducing the need to write SQL directly. It supports various SQL operations like `SELECT`, `FROM`, `JOIN`, `WHERE`, `GROUP BY`, and more.

The SQuery class provides a simplified and efficient way to interact with databases in PHP, specifically by easing the CRUD (Create, Read, Update, Delete) operations.

Features
--------

[](#features)

- SQuery ensures the proper ordering of your SQL syntax, even if the methods are not called in order.
- SQuery supports nearly all functions related to SQL keywords.
- Build SQL queries in a structured and readable manner.
- Supports common SQL operations such as `SELECT`, `FROM`, `JOIN`, `WHERE`, `GROUP BY`, `ORDER BY`, and `LIMIT`.
- Generate complex queries with ease by chaining methods.
- Designed to improve code maintainability and reduce the risk of SQL injection.

Requirements
------------

[](#requirements)

- PHP 8.1 or higher.
- A compatible database (e.g., MySQL, MariaDB) and appropriate database extensions (e.g., MySQLi, PDO) to execute the generated queries.

Installation
------------

[](#installation)

```
composer require ucscode/squery
```

### SELECT SQL EXAMPLE

[](#select-sql-example)

```
use Ucscode\SQuery\SQuery;
use Ucscode\SQuery\Condition;

$squery = new SQuery();

$condition = new Condition();
$condition
   ->add("u.vendor", "ucscode")
   ->and("u.namespace", "SQuery")
   ->or("u.foundation", "Uss%", 'RLIKE');

$squery
   ->select("u.username")
   ->from("tablename", "u")
   ->where($condition)
   ->limit(2)
   ->groupBy('u.id', 'DESC')
   ;

echo $squery->build();
```

### INSERT SQL EXAMPLE

[](#insert-sql-example)

```
$data = [
   'username' => 'Ucscode',
   'password' => '12345',
   'role' => 'SUPER_ADMIN'
];

$squery = new SQuery();
$squery->insert('tablename', $data);
```

### UPDATE SQL EXAMPLE

[](#update-sql-example)

```
$data = [
   'username' => 'User Synthetics',
   'password' => '54321',
   'role' => 'PROJECT'
];

$condition = new Condition();
$condition
   ->add("user_id", 1)
   ->and('role', 'SUPER_ADMIN')
   ->or('username', 'spider-man', 'NOT')
   ->and('finance', null, 'IS NOT');

$squery = new SQuery();
$squery
   ->update('tablename', $data)
   ->where($condition);
```

### DELETE SQL EXAMPLE

[](#delete-sql-example)

5. To generate a DELETE query, use the `delete()` method:

```
$squery = new SQuery();

$condition = new Condition();
$condition->add("username", "trouble-maker");

$squery
   ->delete()
   ->from('tablename')
   ->where($condition);
```

USING JOIN STATEMENT
--------------------

[](#using-join-statement)

SQuery uses an instance of a `Join` object to create and manage different types of join statements. For Example:

### LEFT JOIN EXAMPLE

[](#left-join-example)

```
$squery = new SQuery();

$squery
   ->select([
      "t1.*",
      "t2.value",
      "t1.status",
   ])
   ->from('tablename', "t1");

// Left Join

$leftJoin = new Join('table_2');

$on = (new Condition())
   ->add("t2.user_id", "t1.id")
   ->and("t2.name", null);

$leftJoin->setOn($on);
$leftJoin->setAlias("t2");

$squery->leftJoin($leftJoin);
```

The same applies to other join statement like `RIGHT JOIN`, `INNER JOIN` etc

---

The `Join` object literally accepts an `SQuery` object as it's first parameter, making it capable of handling complex `SQL` statement.

### EXAMPLE

[](#example)

```
$squery = new SQuery();

$squery
   ->select('*')
   ->from('table_1', 't1');

// Create Complex Join Statement;

$rightJoinQuery = (new SQuery())
   ->select("*")
   ->from("table_2")
   ->where(
      (new Condition())
         ->add("name", "ucscode")
         ->and("age", 3, ">")
   )
   ->limit(7);

$onCondition = (new Condition())
   ->add("t1.port", "t2.port", null, false)
   ->or("t1.status", null, 'IS NOT');

$alias = "t2";

$rightJoin = new Join($rightJoinQuery, $onCondition, $alias);

// Add the Right Join Statement

$squery->rightJoin($rightJoin);
```

Warning!
--------

[](#warning)

It is important to note that the SQuery library does not automatically sanitize user input. When using the library, it is crucial to sanitize any user-supplied data before passing it as input to the SQuery methods.

Note
----

[](#note)

Please note that the `SQuery` class only generates SQL query strings; it does not execute them against a database. To execute these queries, you would need to establish a database connection and use appropriate methods from the MySQLi or PDO libraries.

License
-------

[](#license)

This project is licensed under the [MIT License](LICENSE).

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

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

Total

2

Last Release

814d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/65673b1b31e87471999a7614d107e7e061a38bf72191d149c66c1b943124e09c?d=identicon)[ucscode](/maintainers/ucscode)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ucscode-squery/health.svg)

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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