Skip to content

Simple PHP query builder for quick interaction with database tables.

License

Notifications You must be signed in to change notification settings

alirezasalehizadeh/QueryBuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 

Repository files navigation

QueryBuilder

QueryBuilder is a Simple and quick PHP query builder for interaction with database tables using PDO.

Features

  • Simple, light and quick
  • Friendly syntax
  • So useful for pure PHP projects

Requirements

PHP >= 8

Getting Started

Just set your database connection to $connection variable in run() method.

public static function run(): array
    {
        $connection = pdo_connection
        ...
     }

And now, ready to go!

NOTE: You can check the dev branch for more information

CRUD

SELECT
QueryBuilder::table('table_name')->select('name', 'age')->run();
INSERT
QueryBuilder::table('table_name')->insert(
  [

    'name' => 'alex',
    'age' => 20

  ])
->run();
UPDATE
QueryBuilder::table('table_name')->update(['name' => 'john', 'age' => 21])->run();
DELETE
QueryBuilder::table('table_name')->where(['id', '=', 1])->delete()->run();

OTHER EXAMPLES

JOIN
QueryBuilder::table('table_name')->join('second_table_name', ['table_name.id', '=', 'second_table_name.person_id'], 'LEFT')->all()->run();
ORDER BY
QueryBuilder::table('table_name')->all()->orderBy(['table_name.id'], 'DESC')->run();
GROUP BY
QueryBuilder::table('table_name')->all()->groupBy(['table_name.id'])->run();
ALL
QueryBuilder::table('table_name')->all()->run();
FIND
QueryBuilder::table('table_name')->find(1)->run();
AND
QueryBuilder::table('table_name')->all()->where(['id', '=', 1])->and(['name', '=', 'foo'])->run();
OR
QueryBuilder::table('table_name')->all()->where(['id', '=', 1])->or(['name', '=', 'foo'])->run();
LIMIT, OFFSET
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
QUERY
QueryBuilder::setQuery("SELECT * FROM table_name")->run();     //  build your custom query
MAX, MIN, COUNT
//  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();
BETWEEN_OR_NOTBETWEEN
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
IN_OR_NOTIN
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

Contributing

Open issue or send pull request for contributing.

License

MIT.

About

Simple PHP query builder for quick interaction with database tables.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages