Skip to content
A Query Builder for PDO MySQL
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Bloatless Query Builder

A query builder for PDO MySQL.


You can install the library using composer:

php composer.phar require bloatless/endocore-query-builder


Query Builder

This documentation explains the complete usage API of the Bloatless Query Builder.


You can define multiple database connections in your projects config.php file.

$config = [
    'db' => [
        'connections' => [
            'db1' => [
                'driver' => 'mysql',
                'host' => 'localhost',
                'database' => 'db1',
                'username' => 'root',
                'password' => 'your-password',
                'charset' => 'utf8', // Optional
                'timezone' => 'Europe/Berlin', // Optional
            // add additional connections here...
        'default_connection' => 'db1',


The QueryBuilder factory needs to be initialized using a config array providing the connection credentials:

$db = new \Bloatless\Endocore\Components\QueryBuilder\Factory($config['db']);

Once initialized the factory can be used to provide query-builder objects for various database operations:

$selectQueryBuilder = $db->makeSelect();
$updateQueryBuilder = $db->makeUpdate();
$deleteQueryBuilder = $db->makeDelete();
$insertQueryBuilder = $db->makeInsert();
$rawQueryBuilder = $db->makeRaw();

With no arguments provided the default database connection is used. If you want to use a different connection you can pass the connection name as an argument.

$updateQueryBuilder = $db->makeUpdate('db2');


A simple select
$rows = $db->makeSelect()->from('customers')->get();
Table and column alias

Aliases can be used on table names as well as on column names.

$rows = $db->makeSelect()
    ->cols(['customer_id AS id', 'firstname', 'lastname'])
    ->from('customers AS c')
Get specific columns
$rows = $db->makeSelect()
    ->cols(['customer_id', 'firstname', 'lastname'])
First row only
$row = $db->makeSelect()
    ->whereEquals('customer_id', 42)
Single column as array
$names = $db->makeSelect()

Will fetch an array containing all first names of the customers table.

You can specify a second column which will be used for the keys of the array:

$names = $db->makeSelect()
    ->pluck('firstname', 'customer_id');

Will fetch an array of all first names using the customer_id as array key.

Counting rows
$rowCount = $db->makeSelect()

You can join tables using the join, leftJoin or rightJoin methods. You can of course join multiple tables.

$rows = $db->makeSelect()
    ->join('orders', 'customers.customer_id', '=', 'orders.customer_id')
Group by
$rows = $db->makeSelect()
Order by
$rows = $db->makeSelect()
    ->orderBy('firstname', 'desc')
$rows = $db->makeSelect()
    ->having('amount', '>', 10)
    ->orHaving('cart_items', '>' 5)
Limit and Offset
$rows = $db->makeSelect()
$rows = $db->makeSelect()


$rows = $db->makeUpdate()
    ->whereEquals('customer_id', 42)
        'firstname' => 'Homer'


$rows = $db->makeDelete()
    ->whereEquals('customer_id', 42)


You can use various where clauses on all select, update and delete queries:

Simple where
$rows = $db->makeSelect()
    ->where('customer_id', '=', 42)
    ->where('customer_id', '>', 10)
    ->whereEquals('customer_id', 42)
Or where
->orWhere('customer_id', '>', 5)
Where in
->whereIn('customer_id', [1,2,3])
Where not in
->whereNotIn('customer_id', [1,2,3])
Or where in
->orWhereIn('customer_id', [1,2,3])
Or where not in
->orWhereNotIn('customer_id', [1,2,3])
Where between
->whereBetween('customer_id', 5, 10)
Or where between
->orWhereBetween('customer_id', 5, 10)
Where null
Where not null
Or where null
Or where not null


Single row
$customerId = $db->makeInsert()
        'firstname' => 'Homer',
        'lastname' => 'Simpson',

When inserting a single row, the auto-increment value of the newly added row will be returned.

Multiple rows

You can insert multiple rows at once using the rows method:

            'firstname' => 'Homer',
            'lastname' => 'Simpson',
            'firstname' => 'Marge',
            'lastname' => 'Simpson',
Last insert id

In case you need to fetch the id if the last insert manually you can use the getLastInsertId method:

$id = $insertQueryBuilder->getLastInsertId();

RAW Queries

There will always be some kind of queries you can not build using the methods of a query builder. In those cases you can utilize the RawQueryBuilder which allows you to execute raw queries to the database.

Raw select queries
$rows = $db->makeRaw()
    ->prepare("SELECT * FROM `orders` WHERE `customer_id` = :id", [
        'id' => 42,
Other raw queries
    ->prepare("UPDATE `customers` SET `firstname` = :name WHERE `customer_id` = :id", [
        'name' => 'Homer',
        'id' => 42,


All query builders have a reset method. This method can be used to clear all previously set values without the need of creating a new QueryBuilder object.

$builder = $db->makeSelect()
    ->whereEquals('customer_id', 42);




All query builders internally user PDO parameter binding to reduce the risk of injection attacks as much as possible. Additionally table names as well as field names are quoted - so you don't have to worry about that. This works on simple table names or when using aliases. Nevertheless you should always try to avoid using user input within sql statements!



You can’t perform that action at this time.