This is a small easy-to-use PHP component for working with a database by PDO. It provides some public methods to compose SQL queries and manipulate data. Each SQL query is prepared and safe. QueryBuilder fetches data to arrays by default. At present time the component supports MySQL and SQLite (file or memory).
PAY ATTENTION! v0.2 and v0.3+ are incompatible.
Bug reports and/or pull requests are welcome
The package is available as open source under the terms of the MIT license
The preferred way to install this extension is through composer.
Either run
composer require co0lc0der/simple-query-builder
or add
"co0lc0der/simple-query-builder": "*"
to the require section
of your composer.json
file.
$config = require_once __DIR__ . '/config.php';
require_once __DIR__ . '/vendor/autoload.php';
use co0lc0der\QueryBuilder\Connection;
use co0lc0der\QueryBuilder\QueryBuilder;
$query = new QueryBuilder(Connection::make($config['database'])); // $printErrors = false
// for printing errors (since 0.3.6)
$query = new QueryBuilder(Connection::make($config['database']), true)
$results = $query->select('users')->all();
Result query
SELECT * FROM `users`;
$results = $query->select('users')->where([
['id', '>', 1],
'and',
['group_id', 2],
])->all();
Result query
SELECT * FROM `users` WHERE (`id` > 1) AND (`group_id` = 2);
$query->update('posts', ['status' => 'published'])
->where([['YEAR(`updated_at`)', '>', 2020]])
->go();
Result query
UPDATE `posts` SET `status` = 'published'
WHERE (YEAR(`updated_at`) > 2020);
More examples you can find in documentation or tests.
I'm going to add the next features into future versions
- write more unit testes
- add subqueries for QueryBuilder
- add
BETWEEN
- add
WHERE EXISTS
- add TableBuilder class (for beginning
CREATE TABLE
, move$query->drop()
and$query->truncate()
into it) - add PostgreSQL support
- add
WITH
- and probably something more