QueryBuilder is a Simple and quick PHP query builder for interaction with database tables using PDO.
- Simple, light and quick
- Friendly syntax
- So useful for pure PHP projects
PHP >= 8
Just set your database connection to $connection
variable in run()
method.
public static function run(): array
{
$connection = pdo_connection
...
}
And now, ready to go!
QueryBuilder::table('table_name')->select('name', 'age')->run();
QueryBuilder::table('table_name')->insert(
[
'name' => 'alex',
'age' => 20
])
->run();
QueryBuilder::table('table_name')->update(['name' => 'john', 'age' => 21])->run();
QueryBuilder::table('table_name')->where(['id', '=', 1])->delete()->run();
QueryBuilder::table('table_name')->join('second_table_name', ['table_name.id', '=', 'second_table_name.person_id'], 'LEFT')->all()->run();
QueryBuilder::table('table_name')->all()->orderBy(['table_name.id'], 'DESC')->run();
QueryBuilder::table('table_name')->all()->groupBy(['table_name.id'])->run();
QueryBuilder::table('table_name')->all()->run();
QueryBuilder::table('table_name')->find(1)->run();
QueryBuilder::table('table_name')->all()->where(['id', '=', 1])->and(['name', '=', 'foo'])->run();
QueryBuilder::table('table_name')->all()->where(['id', '=', 1])->or(['name', '=', 'foo'])->run();
QueryBuilder::table('table_name')->select('name')->where(['name', '=', 'foo'])->limit(1)->run();
QueryBuilder::table('table_name')->select('name')->where(['name', '=', 'foo'])->limit(1, 3)->run(); // Limit with offset
QueryBuilder::setQuery("SELECT * FROM table_name")->run(); // build your custom query
// Max
QueryBuilder::table('table_name')->select(QueryBuilder::max('user_id', 'id'), 'name')->run();
// Min
QueryBuilder::table('table_name')->select(QueryBuilder::min('user_id', 'id'), 'name')->run();
// Count
QueryBuilder::table('table_name')->select(QueryBuilder::count('user_id', 'id'), 'name')->run();
QueryBuilder::table('table_name')->all()->betweenOrNotBetween('id', 1, 3)->run();
QueryBuilder::table('table_name')->all()->betweenOrNotBetween('id', 1, 3, true)->run(); // True if you want NOT BETWEEN operator
QueryBuilder::table('table_name')->all()->inOrNotIn('id', [1, 3])->run();
QueryBuilder::table('table_name')->all()->inOrNotIn('id', [1, 3], true)->run(); // True if you want NOT IN operator
Open issue or send pull request for contributing.
MIT.