PHP code example of bloatless / query-builder
1. Go to this page and download the library: Download bloatless/query-builder library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bloatless / query-builder example snippets
$factory = new \Bloatless\QueryBuilder\QueryBuilderFactory($myConfig);
$queryBuilder = $factory->make();
$factory = new \Bloatless\QueryBuilder\QueryBuilderFactory($myConfig);
$queryBuilder = $factory->make();
$config = [
'db' => [
'connections' => [
'db1' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db1',
'username' => 'root',
'password' => 'your-password',
'charset' => 'utf8', // Optional
'timezone' => 'Europe/Berlin', // Optional
],
'db2' => [
'driver' => 'sqlite',
'database' => '/path/to/sqlite.db',
]
// add additional connections here...
],
'default_connection' => 'db1',
]
];
$db = new \Bloatless\QueryBuilder\QueryBuilder\Factory($config['db']);
$selectQueryBuilder = $db->makeSelect();
$updateQueryBuilder = $db->makeUpdate();
$deleteQueryBuilder = $db->makeDelete();
$insertQueryBuilder = $db->makeInsert();
$rawQueryBuilder = $db->makeRaw();
$updateQueryBuilder = $db->makeUpdate('db2');
$rows = $db->makeSelect()->from('customers')->get();
$rows = $db->makeSelect()
->cols(['customer_id AS id', 'firstname', 'lastname'])
->from('customers AS c')
->get();
$rows = $db->makeSelect()
->cols(['customer_id', 'firstname', 'lastname'])
->from('customers')
->get();
$row = $db->makeSelect()
->from('customers')
->whereEquals('customer_id', 42)
->first();
$names = $db->makeSelect()
->from('customers')
->pluck('firstname');
$names = $db->makeSelect()
->from('customers')
->pluck('firstname', 'customer_id');
$rowCount = $db->makeSelect()
->from('customers')
->count();
$rows = $db->makeSelect()
->from('customers')
->join('orders', 'customers.customer_id', '=', 'orders.customer_id')
->get();
$rows = $db->makeSelect()
->from('orders')
->groupBy('customer_id')
->get();
$rows = $db->makeSelect()
->from('customers')
->orderBy('firstname', 'desc')
->get();
$rows = $db->makeSelect()
->from('orders')
->having('amount', '>', 10)
->orHaving('cart_items', '>' 5)
->get();
$rows = $db->makeSelect()
->from('orders')
->limit(10)
->offset(20)
->get();
$rows = $db->makeSelect()
->distinct()
->from('orders')
->get();
$rows = $db->makeUpdate()
->table('customers')
->whereEquals('customer_id', 42)
->update([
'firstname' => 'Homer'
]);
$rows = $db->makeDelete()
->from('customers')
->whereEquals('customer_id', 42)
->delete();
$rows = $db->makeSelect()
->from('customers')
->where('customer_id', '=', 42)
->where('customer_id', '>', 10)
->whereEquals('customer_id', 42)
->get();
->orWhere('customer_id', '>', 5)
->whereIn('customer_id', [1,2,3])
->whereNotIn('customer_id', [1,2,3])
->orWhereIn('customer_id', [1,2,3])
->orWhereNotIn('customer_id', [1,2,3])
->whereBetween('customer_id', 5, 10)
->orWhereBetween('customer_id', 5, 10)
->whereNull('customer_id')
->whereNotNull('customer_id')
->orWhereNull('customer_id')
->orWhereNotNull('customer_id')
->whereRaw('TIMESTAMPDIFF(HOUR, `time`, NOW()) <= 24')
->whereRaw('customer_id = :id', ['id' => 10])
->orWhereRaw('TIMESTAMPDIFF(HOUR, `time`, NOW()) <= 24')
$customerId = $db->makeInsert()
->into('customers')
->row([
'firstname' => 'Homer',
'lastname' => 'Simpson',
]);
$db->makeInsert()
->into('customers')
->rows([
[
'firstname' => 'Homer',
'lastname' => 'Simpson',
],
[
'firstname' => 'Marge',
'lastname' => 'Simpson',
],
]);
$id = $insertQueryBuilder->getLastInsertId();
$rows = $db->makeRaw()
->prepare("SELECT * FROM `orders` WHERE `customer_id` = :id", [
'id' => 42,
])
->get();
$db->makeRaw()
->prepare("UPDATE `customers` SET `firstname` = :name WHERE `customer_id` = :id", [
'name' => 'Homer',
'id' => 42,
])
->run();
$builder = $db->makeSelect()
->from('customers')
->whereEquals('customer_id', 42);
$builder->reset();
...