{"id":15178921,"url":"https://github.com/jahidulpabelislam/query","last_synced_at":"2026-01-06T23:22:18.074Z","repository":{"id":45346277,"uuid":"235673973","full_name":"jahidulpabelislam/query","owner":"jahidulpabelislam","description":"Simple database query builder for PHP.","archived":false,"fork":false,"pushed_at":"2025-01-17T00:02:23.000Z","size":96,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"2.x","last_synced_at":"2025-01-31T22:11:26.113Z","etag":null,"topics":["database","mysql","php","query-builder"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jahidulpabelislam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-22T21:47:06.000Z","updated_at":"2024-09-06T23:40:58.000Z","dependencies_parsed_at":"2024-06-01T01:22:18.110Z","dependency_job_id":"7fa31008-c87e-4ef9-bbc3-91f2acb8a6a8","html_url":"https://github.com/jahidulpabelislam/query","commit_stats":{"total_commits":100,"total_committers":3,"mean_commits":"33.333333333333336","dds":"0.17000000000000004","last_synced_commit":"140fa291a9cebc2a133aa0046ff643f2508b1a0b"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fquery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fquery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fquery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fquery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jahidulpabelislam","download_url":"https://codeload.github.com/jahidulpabelislam/query/tar.gz/refs/heads/2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238386075,"owners_count":19463291,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","mysql","php","query-builder"],"created_at":"2024-09-27T15:42:00.274Z","updated_at":"2026-01-02T02:04:08.384Z","avatar_url":"https://github.com/jahidulpabelislam.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Query\n\n[![CodeFactor](https://www.codefactor.io/repository/github/jahidulpabelislam/query/badge)](https://www.codefactor.io/repository/github/jahidulpabelislam/query)\n[![Latest Stable Version](https://poser.pugx.org/jpi/query/v/stable)](https://packagist.org/packages/jpi/query)\n[![Total Downloads](https://poser.pugx.org/jpi/query/downloads)](https://packagist.org/packages/jpi/query)\n[![Latest Unstable Version](https://poser.pugx.org/jpi/query/v/unstable)](https://packagist.org/packages/jpi/query)\n[![License](https://poser.pugx.org/jpi/query/license)](https://packagist.org/packages/jpi/query)\n![GitHub last commit (branch)](https://img.shields.io/github/last-commit/jahidulpabelislam/query/2.x.svg?label=last%20activity)\n\nA simple and lightweight SQL query builder library to make querying a database easier. It works as a middleman between your application and the database.\n\nThis has been kept very simple stupid (KISS), with minimal validation (PHP type errors only) to reduce complexity in the library and maximize performance for consumer developers. Therefore, please make sure to add your own validation if using user inputs in these queries.\n\n## Features\n\n- Fluent, chainable query builder with simple, expressive syntax\n- Support for `SELECT`, `INSERT`, `UPDATE`, and `DELETE` queries\n- Support for columns, joins (`INNER`, `LEFT`, and `RIGHT`), where clauses, ordering, limiting, and paging\n- Returns convenient collections for `SELECT` queries\n\n## Dependencies\n\n- PHP 8.0+\n- Composer\n- PHP PDO\n- MySQL 5+\n- [jpi/database](https://packagist.org/packages/jpi/database) v2\n\n## Installation\n\nUse [Composer](https://getcomposer.org/)\n\n```bash\n$ composer require jpi/query \n```\n\n## Usage\n\nTo create an instance, you will need an instance of `\\JPI\\Database` (which is an extension of `PDO` - you can find out more [here](https://packagist.org/packages/jpi/database)) which is the first parameter, and the database table name as the second parameter. The same instance can be used multiple times as long as it's for the same database.\n\n```php\n$queryBuilder = new \\JPI\\Database\\Query\\Builder($database, $table);\n```\n\n### Action Methods:\n\nThese are the methods to call to end with `select`, `count: int`, `insert($values array): int|null`, `update($values array): int` \u0026 `delete: int`, all are pretty self-explanatory.\n\n### Builder methods\n\nThese are all fluent methods, so you can chain them together.\n\n- `table(string $table, string|null $alias)`: if you want to change to another table or didn't set when creating the instance\n- `column(string $column, string|null $alias)`:  will select all columns if not set\n- `join($joinOrTable, string|null $on, string $type)`:\n    - `$joinOrTable`: instance of `\\JPI\\Database\\Query\\Clause\\Join` or the table name as string, use the class if you want multiple expressions in the `ON` clause\n    - `type`: `INNER` (default), `LEFT` or `RIGHT`, usually you can leave blank, and use `rightJoin` or `leftJoin` methods\n- `where`:\n    - you can pass in the whole clause using the first parameter\n    - or you can pass column, expression and value separately\n- `orderBy(string $column, bool $ascDirection = true)`\n- `limit(int $limit, int|null $page)`\n- `page(int)`: used to change the offset, only used if `limit` set\n\n### Examples\n\nAssuming a `\\JPI\\Database\\Query\\Builder` instance has been created for the `users` database table and set to a variable named `$queryBuilder`.\n\n#### select\n\nThis has 4 return types depending on how you use it:\n\n- if you've set `limit` of `1` this will return an associative array of key (column) value pairs or if not found then `null`\n- if paged `\\JPI\\Database\\Query\\PaginatedResult`\n- else `\\JPI\\Database\\Query\\Result`\n\n`PaginatedResult` \u0026 `Result` work like a normal array just with some extra methods, see https://github.com/jahidulpabelislam/utils?tab=readme-ov-file#collection for more details.\n\n```php\n// SELECT * FROM users;\n$collection = $queryBuilder-\u003eselect();\n/**\n$collection = [\n    [\n        \"id\" =\u003e 1,\n        \"first_name\" =\u003e \"Jahidul\",\n        \"last_name\" =\u003e \"Islam\",\n        \"email\" =\u003e \"jahidul@jahidulpabelislam.com\",\n        \"password\" =\u003e \"password123\",\n        ...\n    ],\n    [\n        \"id\" =\u003e 2,\n        \"first_name\" =\u003e \"Test\",\n        \"last_name\" =\u003e \"Example\",\n        \"email\" =\u003e \"test@example.com\",\n        \"password\" =\u003e \"password123\",\n        ...\n    ],\n    ...\n];\n*/\n\n// SELECT first_name, last_name FROM users;\n$collection = $queryBuilder\n    -\u003ecolumn(\"first_name\")\n    -\u003ecolumn(\"last_name\")\n    -\u003eselect();\n/**\n$collection = [\n    [\n        \"first_name\" =\u003e \"Jahidul\",\n        \"last_name\" =\u003e \"Islam\",\n    ],\n    [\n        \"first_name\" =\u003e \"Test\",\n        \"last_name\" =\u003e \"Example\",\n    ],\n    ...\n];\n*/\n\n// SELECT * FROM users WHERE status = \"active\";\n$collection = $queryBuilder\n    -\u003ewhere(\"status\", \"=\", \"active\")\n    -\u003eselect();\n/**\n$collection = [\n    [\n        \"id\" =\u003e 1,\n        \"first_name\" =\u003e \"Jahidul\",\n        \"last_name\" =\u003e \"Islam\",\n        \"email\" =\u003e \"jahidul@jahidulpabelislam.com\",\n        \"password\" =\u003e \"password123\",\n        \"status\" =\u003e \"active\",\n        ...\n    ],\n    [\n        \"id\" =\u003e 3,\n        \"first_name\" =\u003e \"Test\",\n        \"last_name\" =\u003e \"Example\",\n        \"email\" =\u003e \"test@example.com\",\n        \"password\" =\u003e \"password123\",\n        \"status\" =\u003e \"active\",\n        ...\n    ],\n    ...\n];\n*/\n\n// SELECT * FROM users WHERE status = \"active\" ORDER BY last_name ASC;\n$collection = $queryBuilder\n    -\u003ewhere(\"status\", \"=\", \"active\")\n    -\u003eorderBy(\"last_name\")\n    -\u003eselect();\n/**\n$collection = [\n    [\n        \"id\" =\u003e 3,\n        \"first_name\" =\u003e \"Test\",\n        \"last_name\" =\u003e \"Example\",\n        \"email\" =\u003e \"test@example.com\",\n        \"password\" =\u003e \"password123\",\n        \"status\" =\u003e \"active\",\n        ...\n    ],\n    [\n        \"id\" =\u003e 1,\n        \"first_name\" =\u003e \"Jahidul\",\n        \"last_name\" =\u003e \"Islam\",\n        \"email\" =\u003e \"jahidul@jahidulpabelislam.com\",\n        \"password\" =\u003e \"password123\",\n        \"status\" =\u003e \"active\",\n        ...\n    ],\n    ...\n];\n*/\n\n// SELECT * FROM users WHERE status = \"active\" ORDER BY first_name ASC LIMIT 10 OFFSET 20;\n$collection = $queryBuilder\n    -\u003ewhere(\"status\", \"=\", \"active\")\n    -\u003eorderBy(\"first_name\")\n    -\u003elimit(10, 3)\n    -\u003eselect();\n/**\n$collection = [\n    [\n        \"id\" =\u003e 31,\n        \"first_name\" =\u003e \"Jahidul\",\n        \"last_name\" =\u003e \"Islam\",\n        \"email\" =\u003e \"jahidul@jahidulpabelislam.com\",\n        \"password\" =\u003e \"password123\",\n        \"status\" =\u003e \"active\",\n        ...\n    ],\n    [\n        \"id\" =\u003e 30,\n        \"first_name\" =\u003e \"Test\",\n        \"last_name\" =\u003e \"Example\",\n        \"email\" =\u003e \"test@example.com\",\n        \"password\" =\u003e \"password123\",\n        \"status\" =\u003e \"active\",\n        ...\n    ],\n    ...\n];\n*/\n\n// SELECT * FROM users WHERE first_name LIKE \"%jahidul%\" LIMIT 1;\n$row = $queryBuilder\n    -\u003ewhere(\"first_name\", \"LIKE\", \"%jahidul%\")\n    -\u003elimit(1)\n    -\u003eselect();\n/**\n$row = [\n    \"id\" =\u003e 1,\n    \"first_name\" =\u003e \"Jahidul\",\n    \"last_name\" =\u003e \"Islam\",\n    \"email\" =\u003e \"jahidul@jahidulpabelislam.com\",\n    \"password\" =\u003e \"password\",\n    ...\n];\n*/\n\n/**\nSELECT * FROM users\nINNER JOIN user_logins ON user_id = login_user_user_id;\n*/\n$queryBuilder-\u003ejoin(\"user_logins\", \"user_id = login_user_user_id\");\n$collection = $queryBuilder-\u003eselect();\n/**\n$collection = [\n    [\n        \"id\" =\u003e 1,\n        \"first_name\" =\u003e \"Jahidul\",\n        \"login_user_id\" =\u003e 1,\n        \"login_user_user_id\" =\u003e 1,\n        \"login_user_date\" =\u003e \"2025-10-29 10:00:00\",\n        ...\n    ],\n    [\n        \"id\" =\u003e 1,\n        \"first_name\" =\u003e \"Jahidul\",\n        \"login_user_id\" =\u003e 2,\n        \"login_user_user_id\" =\u003e 1,\n        \"login_user_date\" =\u003e \"2025-11-01 12:00:00\",\n        ...\n    ],\n];\n\n/**\nSELECT * FROM users\nINNER JOIN user_logins ON user_id = login_user_user_id AND login_user_date \u003e '2025-11-01';\n */\n$queryBuilder-\u003ejoin(\n    $queryBuilder-\u003enewJoinClause(\"user_logins\")\n        -\u003eon(\"user_id = login_user_user_id\")\n        -\u003eon(\"login_user_date \u003e '2025-11-01'\")\n);\n$queryBuilder-\u003eselect();\n/**\n$collection = [\n    [\n        \"id\" =\u003e 1,\n        \"first_name\" =\u003e \"Jahidul\",\n        \"login_user_id\" =\u003e 2,\n        \"login_user_user_id\" =\u003e 1,\n        \"login_user_date\" =\u003e \"2025-11-01 12:00:00\",\n        ...\n    ],\n];\n```\n\n#### count\n\nAs the name implies this method will just return the count as an integer.\n\nFor obvious reasons only the `table` \u0026 `where` builder methods are supported for this action.\n\n```php\n// SELECT COUNT(*) as count FROM users;\n$count = $queryBuilder-\u003ecount();\n// $count = 10;\n\n// SELECT COUNT(*) as count FROM users WHERE status = \"active\";\n$count = $queryBuilder\n    -\u003ewhere(\"status\", \"=\", \"active\")\n    -\u003ecount();\n// $count = 5;\n```\n\n#### insert\n\nThis method will just return the id of the row created unless it fails then `null`.\n\nOnly the `table` builder method is supported for this action.\n\n```php\n// INSERT INTO users SET first_name= \"Jahidul\", last_name= \"Islam\", email = \"jahidul@jahidulpabelislam.com\", password = \"password\";\n$id = $queryBuilder-\u003einsert([\n    \"first_name\" =\u003e \"Jahidul\",\n    \"last_name\" =\u003e \"Islam\",\n    \"email\" =\u003e \"jahidul@jahidulpabelislam.com\",\n    \"password\" =\u003e \"password\",\n]);\n// $id = 1;\n```\n\n#### update\n\nThis method will return the count of how many rows have been updated by the query.\n\n`column` \u0026 `page` builder methods aren't supported for this action.\n\n```php\n// UPDATE users SET status = \"inactive\";\n$numberOrRowsUpdated = $queryBuilder-\u003eupdate([\n    \"status\" =\u003e \"inactive\",\n]);\n// $numberOrRowsUpdated = 10;\n\n// UPDATE users SET first_name = \"Pabel\" WHERE id = 1;\n$numberOrRowsUpdated = $queryBuilder\n    -\u003ewhere(\"id\", \"=\", 1)\n    -\u003eupdate([\n        \"first_name\" =\u003e \"Pabel\",\n    ]);\n// $numberOrRowsUpdated = 1;\n```\n\n#### delete\n\nThis method will return the count of how many rows have been deleted by the query.\n\n`column` \u0026 `page` builder methods aren't supported for this action.\n\n```php\n// DELETE FROM users;\n$numberOrRowsDeleted = $queryBuilder-\u003edelete();\n// $numberOrRowsDeleted = 10;\n\n// DELETE FROM users WHERE id = 1;\n$numberOrRowsDeleted = $queryBuilder\n    -\u003ewhere(\"id\", \"=\", 1)\n    -\u003edelete();\n// $numberOrRowsDeleted = 1;\n```\n\n## Support\n\nIf you found this library interesting or useful please spread the word about this library: share on your socials, star on GitHub, etc.\n\nIf you find any issues or have any feature requests, you can open an [issue](https://github.com/jahidulpabelislam/query/issues) or email [me @ jahidulpabelislam.com](mailto:me@jahidulpabelislam.com) :smirk:.\n\n## Authors\n\n- [Jahidul Pabel Islam](https://jahidulpabelislam.com/) [\u003cme@jahidulpabelislam.com\u003e](mailto:me@jahidulpabelislam.com)\n\n## Licence\n\nThis module is licensed under the General Public Licence - see the [licence](LICENSE.md) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjahidulpabelislam%2Fquery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjahidulpabelislam%2Fquery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjahidulpabelislam%2Fquery/lists"}