PHPackages                             maghead/magsql - 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. maghead/magsql

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

maghead/magsql
==============

Fast and Powerful SQL for PHP

46392[2 issues](https://github.com/maghead/magsql/issues)4PHP

Since Jan 11Pushed 8y agoCompare

[ Source](https://github.com/maghead/magsql)[ Packagist](https://packagist.org/packages/maghead/magsql)[ RSS](/packages/maghead-magsql/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (4)

Magsql - the powerful sql builder designed for PHP7
===================================================

[](#magsql---the-powerful-sql-builder-designed-for-php7)

[![Build Status](https://camo.githubusercontent.com/d39428cb596f35c22d01b3254f506599259ecb68e07bdc637d8886c56e74259e/68747470733a2f2f7472617669732d63692e6f72672f6d6167686561642f6d616773716c2e7376673f6272616e63683d6d6173746572)](http://travis-ci.org/maghead/magsql)[![Coverage Status](https://camo.githubusercontent.com/132b25845d43bad980a7094bf18eaf36c59dc3e0a9b2d1a74eca497427d46622/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6d6167686561642f6d616773716c2e737667)](https://coveralls.io/r/maghead/magsql)[![Latest Stable Version](https://camo.githubusercontent.com/9d6304d22578a8010a5ef5856cbd4f496a86adb0f5cc505f0d4bd4a43321d99d/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f73716c6275696c6465722f762f737461626c652e737667)](https://packagist.org/packages/corneltek/sqlbuilder)[![Total Downloads](https://camo.githubusercontent.com/71d2d50ead5e46d900ce08424d461ffbc66a62e10f48f229345ea49ff3d1ea64/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f73716c6275696c6465722f646f776e6c6f6164732e737667)](https://packagist.org/packages/corneltek/sqlbuilder)[![Monthly Downloads](https://camo.githubusercontent.com/521a13b36ad2039013c043ffad9b48c2fa626b53e27c7273e724e6794e6bd219/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f73716c6275696c6465722f642f6d6f6e74686c79)](https://packagist.org/packages/corneltek/sqlbuilder)[![Latest Unstable Version](https://camo.githubusercontent.com/42d50ec1178e45af59d33f1d6bede53bbcb081a1b310de35c4f86884e42ac534/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f73716c6275696c6465722f762f756e737461626c652e737667)](https://packagist.org/packages/corneltek/sqlbuilder)[![License](https://camo.githubusercontent.com/d9694feba7ca51351ce28a12df633adb3a9a069a77c2118746a12aa587d143d8/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f73716c6275696c6465722f6c6963656e73652e737667)](https://packagist.org/packages/corneltek/sqlbuilder)

If you're looking for something that is not an ORM but can generate SQL for you, you just found the right one.

Magsql is not an ORM (Object relational mapping) system, but a toolset that helps you generate cross-platform SQL queries in PHP.

Magsql is a stand-alone library, you can simply install it through composer or just require them (the class files) with your autoloader, and it has no dependencies.

Features
--------

[](#features)

- Simple API, easy to remember.
- Fast &amp; Powerful.
- Custom parameter marker support:
    - Question-mark parameter marker.
    - Named parameter marker.
- Configurable quote handler.
- Zero dependency.

Synopsis
--------

[](#synopsis)

Here is a short example of using Universal SelectQuery

```
use Magsql\Universal\Query\SelectQuery;
use Magsql\Driver\MySQLDriver;
use Magsql\Driver\PgSQLDriver;
use Magsql\Driver\SQLiteDriver;

$mysql = new MySQLDriver;
$args = new ArgumentArray;

$query = new SelectQuery;
$query->select(array('id', 'name', 'phone', 'address','confirmed'))
    ->from('users', 'u')
    ->partitions('u1', 'u2', 'u3')
    ->where()
        ->is('confirmed', true)
        ->in('id', [1,2,3])
    ;
$query
    ->join('posts')
        ->as('p')
        ->on('p.user_id = u.id')
    ;
$query
    ->orderBy('rand()')
    ->orderBy('id', 'DESC')
    ;

$sql = $query->toSql($mysql, $args);

var_dump($sql);
var_dump($args);
```

A More Detailed Description
---------------------------

[](#a-more-detailed-description)

Unlike other SQL utilities, Magsql let you define the quote style and the parameter marker type. there are 2 parameter marker type you can choose:

1. Question mark parameter marker (`?`)
2. Named parameter. (`:id`, `:name`, `:address`, `:p1`)

The above two are supported by PDO directly, and the first one is also supported by `mysqli`, `pgsql` extension.

The API is *dead simple, easy to remember*, you can just define one query, then pass different query driver to the query object to get a different SQL string for your targettting platform.

It also supports cross-platform query generation, there are three types of query (currently): **Universal**, **MySQL**, **PgSQL**. The **Universal** queries are cross-platform, you can use them to create a cross-platform PHP API of your database system, and the supported platforms are: **MySQL**, **PgSQL** and **SQLite**.

Universql Queries:

- CreateDatabaseQuery
- DropDatabaseQuery
- SelectQuery
- InsertQuery
- UpdateQuery
- DeleteQuery
- UnionQuery
- CreateIndexQuery
- DropIndexQuery

To see the implementation details, you can check the source code inside **Universal** namespace:

MySQL Queries:

- CreateUserQuery
- DropUserQuery
- GrantQuery
- SetPasswordQuery

For MySQL platform, the implementation is according to the specification of MySQL 5.6.

For PostgreSQL platform, the implementation is according to the specification of PostgreSQL 9.2.

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

[](#installation)

### Install through Composer

[](#install-through-composer)

```
composer require corneltek/sqlbuilder

```

Getting Started
---------------

[](#getting-started)

[Documentation](https://github.com/maghead/magsql/wiki)

Development
-----------

[](#development)

```
composer install

```

Copy the `phpunit.xml` file for your local configuration:

```
phpunit -c your-phpunit.xml tests
```

Contribution
------------

[](#contribution)

To test with mysql database:

```
mysql -uroot -p
CREATE DATABASE sqlbuilder CHARSET utf8;
GRANT ALL PRIVILEGES ON sqlbuilder.* TO 'testing'@'localhost' identified by '';

--- or use this to remove password for testing account
SET PASSWORD FOR testing@localhost=PASSWORD('');

```

To test with pgsql database:

```
sudo -u postgres createdb sqlbuilder

```

Reference
---------

[](#reference)

-
-
-

Author
------

[](#author)

Yo-An Lin (c9s)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 99.3% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/3cc34cde233b660869ff329ed8e20df611f75dfb61aab3e30889ac153d3e5e61?d=identicon)[c9s](/maintainers/c9s)

---

Top Contributors

[![c9s](https://avatars.githubusercontent.com/u/50894?v=4)](https://github.com/c9s "c9s (1041 commits)")[![jancinert](https://avatars.githubusercontent.com/u/5624510?v=4)](https://github.com/jancinert "jancinert (2 commits)")[![kbinani](https://avatars.githubusercontent.com/u/1030301?v=4)](https://github.com/kbinani "kbinani (2 commits)")[![joushx](https://avatars.githubusercontent.com/u/832178?v=4)](https://github.com/joushx "joushx (1 commits)")[![krichprollsch](https://avatars.githubusercontent.com/u/562696?v=4)](https://github.com/krichprollsch "krichprollsch (1 commits)")[![SmetDenis](https://avatars.githubusercontent.com/u/1118678?v=4)](https://github.com/SmetDenis "SmetDenis (1 commits)")

---

Tags

sqluniversal

### Embed Badge

![Health badge](/badges/maghead-magsql/health.svg)

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

###  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)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

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

Reliese Components for Laravel Framework code generation.

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

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

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