ClanCats Logo



🐉 Fast & standalone PHP MySQL Query Builder library.

Star Watch


Hydrahon is a standalone database query builder written in PHP. It was built to enhance existing frameworks, libraries and applications that handle the database connection on their own. It does not come with a PDO or mysqli wrapper. The naming is heavily inspired by Eloquent and the Kohana Framework Database component.

What does that mean "Standalone query builder"?

Basically, Hydrahon only generated a query string and an array of parameters. On its own, it is not able to actually execute a query.

Build Status Packagist Packagist GitHub release


  • The Hydrahon MySQL query builder is stable and used in production.
  • The Hydrahon AQL (Arango Query Langauge) query builder is currently in development.
  • A builder for Elasticsearch is on my mind but not in development.


Hydrahon follows PSR-4 autoloading and can be installed using composer:

$ composer require 'clancats/hydrahon'

Documentation 💡

The full documentation can be found on

Quick Start (MySQL) ⚡️

Hydrahon is designed to be a pretty generic query builder. So for this quick start, we stick with SQL.

Create a builder

Again this library is not built as a full database abstraction or ORM, it is only and will always be only a query builder. This means we need to implement the database connection and fetching by ourselves.

In this example, we are going to use PDO

$connection = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password');

// create a new mysql query builder
$h = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryString, $queryParameters) use($connection)
    $statement = $connection->prepare($queryString);

    // when the query is fetchable return all results and let hydrahon do the rest
    if ($query instanceof \ClanCats\Hydrahon\Query\Sql\FetchableInterface)
        return $statement->fetchAll(\PDO::FETCH_ASSOC);

And we are ready and set. The variable $h contains now a MySQL query builder.

Setup a simple table:

To continue with our examples we need to create a simple mysql table.

CREATE TABLE `people` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT '',
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)


Currently, we don't have any data, to fix this let's go and insert some.

// In our example we are going to execute multiple operations on the same table, 
// so instead of loading the table over and over again, we store it in a variable.
$people = $h->table('people');

    ['name' => 'Ray', 'age' => 25],
    ['name' => 'John',  'age' => 30],
    ['name' => 'Ali', 'age' => 22],

Will execute the following query:

insert into `people` (`age`, `name`) values (?, ?), (?, ?), (?, ?)

As you can see the Hydrahon automatically escapes parameters.

But because we are humans that get confused when there are hundreds of thousands of questions marks, I will continue to always show the runnable query:

insert into `people` (`age`, `name`) values (25, Ray), (30, John), (22, Ali)


Ah snap, time runs so fast, "Ray" is actually already 26.

    ->set('age', 26)
    ->where('name', 'Ray')


update `people` set `age` = 26 where `name` = Ray

Currently, you might think: "Well isn't it much simpler to just write the SQL query? I mean the PHP code is even longer...".

You have to understand that these are some very very basic examples the Hydrahon query builder starts to shine when things get more complex. But a "Quick Start" is in my view is just the wrong place for complex stuff, so throw an eye on the full documentation.


Dammit John, I hate you...

    ->where('name', 'John')


delete from `people` where `name` = John


And finally, fetch the data.



select * from `people`


    "id": "1",
    "name": "Ray",
    "age": "26"
    "id": "3",
    "name": "Ali",
    "age": "22"



The MIT License (MIT). Please see License File for more information.