{"id":22612320,"url":"https://github.com/utsavdotpro/phpqueryutils","last_synced_at":"2025-03-28T23:44:51.288Z","repository":{"id":52424631,"uuid":"341634290","full_name":"utsavdotpro/PHPQueryUtils","owner":"utsavdotpro","description":"Utils class for easily creating/executing MySQLi queries in PHP.","archived":false,"fork":false,"pushed_at":"2021-05-31T12:53:32.000Z","size":26,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-03T10:12:08.936Z","etag":null,"topics":["mysqli-queries","php","php-query-builder","php-query-utils","sql-queries"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/utsavdotpro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-23T17:27:42.000Z","updated_at":"2022-12-01T18:40:42.000Z","dependencies_parsed_at":"2022-08-30T15:30:27.766Z","dependency_job_id":null,"html_url":"https://github.com/utsavdotpro/PHPQueryUtils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utsavdotpro%2FPHPQueryUtils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utsavdotpro%2FPHPQueryUtils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utsavdotpro%2FPHPQueryUtils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utsavdotpro%2FPHPQueryUtils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/utsavdotpro","download_url":"https://codeload.github.com/utsavdotpro/PHPQueryUtils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246117690,"owners_count":20726068,"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":["mysqli-queries","php","php-query-builder","php-query-utils","sql-queries"],"created_at":"2024-12-08T17:12:17.324Z","updated_at":"2025-03-28T23:44:51.262Z","avatar_url":"https://github.com/utsavdotpro.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHPQueryUtils\nSimple `Query` class to easily and quickly write/execute MySQLi queries.  \n\nYou no more need to write specific queries for every table and every operation. Just create a data object pass with your table name through appropriate function and voila, it's done!  \n\n---\n  \nCheckout different operations available:\n\n  - [raw](#raw): run any query, get data array\n  - [rawForResult](#rawforresult): run any query, get result object\n  - [select](#select): get first row from table as object\n  - [selectWhere](#selectwhere): get any row from table as object\n  - [insert](#insert): add new row to table\n  - [insertMultiple](#insertMultiple): add new rows to table\n  - [replace](#replace): add new or replace existing row in table\n  - [delete](#delete): delete any row from table\n  - [updateWhere](#updatewhere): update any row in table\n  - [updateMultiple](#updatemultiple): update multiple rows in the table\n  - [iterateOnResult](#iterateonresult): loop on selected rows from table, with empty callback\n  - [truncate](#truncate): delete all rows from table\n  - [upsert](#upsert): update or insert row in table\n\n---\n\n## raw\n`raw($query)`  \n\nSome queries are just too complex to create objects for, just write it yourself and pass it to the raw()\n\n````php\nQuery::raw(\n  \"SELECT e.*, a.level FROM employees e\n  LEFT OUTER JOIN authorization a ON a.emp_id=e.id\n  WHERE e.id=12\"\n);\n\n\n// returns: boolean || data array\n````\n\n````sql\n/* query executed: whatever query you wrote */\nSELECT e.*,\n       a.level\nFROM employees e\nLEFT OUTER JOIN\nAUTHORIZATION a ON a.emp_id=e.id\nWHERE e.id=12\n````\n\n## rawForResult\n`rawForResult($query)`  \n\nNot for general use but in case you want the result object instead of data array, rawForResult() is your buddy\n\n````php\nQuery::rawForResult(\n  \"SELECT e.*, a.level FROM employees e\n  LEFT OUTER JOIN authorization a ON a.emp_id=e.id\n  WHERE e.id=12\"\n);\n\n// returns: boolean || result object\n````\n\n````sql\n/* query executed: whatever query you wrote */\nSELECT e.*,\n       a.level\nFROM employees e\nLEFT OUTER JOIN\nAUTHORIZATION a ON a.emp_id=e.id\nWHERE e.id=12\n````\n\n## select\n`select($tableName)`  \n\nPass the name of your table and get an data object of first row. Useful when selecting from config tables\n\n````php\nQuery::select(\"settings\");\n\n// returns: data object\n````\n\n\n````sql\n/* query executed */\nSELECT *\nFROM settings\nWHERE 1=1\n````\n\n## selectWhere\n`selectWhere($tableName, $whereString, $whereIdValue = null)`  \n\nReturns first array from the table where the rows match conditions in whereString.  \nYou can directly separate all your conditions with whatever relation you like `AND` or `OR` . Check the example below \nComparing rows with id value is one of the most common use case so there's a special filter for it, just pass the id as a third parameter and it will add \u003ci\u003e AND \\`id\\`='$your_passed_id'\u003c/i\u003e (see example below)\n\n````php\nQuery::selectWhere(\n  \"employees\",\n  \"`email`='example@email.com' AND `password`='@123'\",\n  1\n);\n\n// returns: data object\n````\n\n````sql\n/* query executed */\nSELECT *\nFROM employees\nWHERE `email`='example@email.com'\n  AND `password`='@123'\n  AND `id`='1'\n````\n\n## insert\n`insert($table, $dataObject, $ignoreMode = false)`  \n\nTo add a new row to your table use insert(). First create your data object (see example below) and then just pass it to the function.  \nIf you want to run `INSERT IGNORE INTO` instead of `INSERT INTO`, just enable ignoreMode using the third parameter.\n\n````php\n$data = [\n  \"first_name\" =\u003e \"Abdul\",\n  \"last_name\" =\u003e \"Kalam\",\n];\n\nQuery::insert(\"users\", $data);  \n\n// returns: boolean || integer (inserted row id)\n````\n\n````sql\n/* query executed */\nINSERT INTO users (`first_name`, `last_name`)\nVALUES ('Abdul', 'Kalam')\n````\n\n## insertMultiple\n`insertMultiple($table, $dataArray, $ignoreMode = false, $column = \"\", $columnId = 0)`  \n\nTo add multiple rows to your table use insertMultiple(). First create your data array (see example below) and then just pass it to the function.  \nIf you want to run `INSERT IGNORE INTO` instead of `INSERT INTO`, just enable ignoreMode using the third parameter.  \nThis is generally to be used to insert mappings, so if you want to pass the reference column id additionally, you can use the 4th and 5th parameters.\n\n`````php\n$data = [\n  [\n    \"first_name\" =\u003e \"Abdul\",\n    \"last_name\" =\u003e \"Kalam\",\n  ],\n  [\n    \"first_name\" =\u003e \"C.V.\",\n    \"last_name\" =\u003e \"Raman\",\n  ],\n  [\n    \"first_name\" =\u003e \"Srinivasa\",\n    \"last_name\" =\u003e \"Ramanujan\",\n  ],\n];\n\nQuery::insertMultiple(\"users\", $data);  \n\n// returns: boolean\n`````\n\n`````sql\n/* query executed */\nINSERT INTO users (`first_name`, `last_name`)\nVALUES ('Abdul', 'Kalam'),\n       ('C.V.', 'Raman'),\n       ('Srinivasa', 'Ramanujan')\n`````\n\n## replace\n`replace($table, $dataObject)`  \n\nTo replace existing (or add new) row to your table, use replace()\n\n`````php\n$data = [\n  \"first_name\" =\u003e \"Abdul\",\n  \"last_name\" =\u003e \"Kalam\",\n];\n\nQuery::replace(\"users\", $data);  \n\n// returns: boolean || integer (inserted row id)\n`````\n\n`````sql\n/* query executed */\nREPLACE INTO users (`first_name`, `last_name`)\nVALUES ('Abdul', 'Kalam')\n`````\n\n## delete\n`delete($table, $whereString, $whereIdValue = null)`  \n\nDelete the rows from the table where the condition in whereString is matched.  \nYou can directly separate all your conditions with whatever relation you like `AND` or `OR` . Check the example below \nComparing rows with id value is one of the most common use case so there's a special filter for it, just pass the id as a third parameter and it will add \u003ci\u003e AND \\`id\\`='$your_passed id'\u003c/i\u003e\n\n`````php\nQuery::delete(\n  \"employees\",\n  \"`email`='example@email.com' AND `password`='@123'\"\n);\n\n// returns: boolean\n`````\n\n`````sql\n/* query executed */\nDELETE\nFROM employees\nWHERE `email`='example@email.com'\n  AND `password`='@123'\n`````\n\n## updateWhere\n`updateWhere($table, $dataObject, $whereString, $whereIdValue = null)`  \n\nFirst create a data object and pass it to the updateWhere() and the rows from the table where the condition in whereString is matched are updated.  \nYou can directly separate all your conditions with whatever relation you like `AND` or `OR`   \nComparing rows with id value is one of the most common use case so there's a special filter for it, just pass the id as a third parameter and it will add \u003ci\u003e AND \\`id\\`='$your_passed id'\u003c/i\u003e\n\n`````php\n$data = [\n  \"first_name\" =\u003e \"Abdul\",\n  \"last_name\" =\u003e \"Kalam\",\n];\n\nQuery::updateWhere(\n  \"users\",\n  \"`email`='example@email.com'\"\n);\n\n// returns: boolean\n`````\n\n`````sql\n/* query executed */\nUPDATE users\nSET `first_name`='Abdul',\n    `last_name`='Kalam'\nWHERE `email`='example@email.com'\n`````\n\n## updateMultiple\n`updateMultiple($table, $dataArray)`  \n\nTo update multiple rows in your table use updateMultiple(). First create your data array (see example below) and then just pass it to the function.  \n  \n**Note: for this to be able to work, your table must have at least one column with `PRIMARY` OR `UNIQUE` key and you must keep that column in the data array.**\n\n`````php\n$data = [\n  [\n    \"id\" =\u003e 1, // here id column has PRIMARY key\n    \"first_name\" =\u003e \"Abdul\",\n    \"last_name\" =\u003e \"Kalam\",\n  ],\n  [\n    \"id\" =\u003e 2,\n    \"first_name\" =\u003e \"C.V.\",\n    \"last_name\" =\u003e \"Raman\",\n  ],\n  [\n    \"id\" =\u003e 3,\n    \"first_name\" =\u003e \"Srinivasa\",\n    \"last_name\" =\u003e \"Ramanujan\",\n  ],\n];\n\nQuery::updateMultiple(\"users\", $data);  \n\n// returns: boolean\n`````\n\n`````sql\n/* query executed */\nINSERT INTO users (`id`, `first_name`, `last_name`)\nVALUES (1, 'Abdul', 'Kalam'),\n       (2, 'C.V.', 'Raman'),\n       (3, 'Srinivasa', 'Ramanujan')\nON DUPLICATE KEY UPDATE id=VALUES(id),\n  first_name=VALUES(first_name),\n  last_name=VALUES(last_name)\n`````\n\n\u003e We're using multi INSERT query here in the UPDATE mode so basically, if a column exists, it will be updated. And if it doesn't exists, it will be created.  \n\u003e So be sure that you only pass the column that already exist in the table until unless you **knowingly** don't want to.\n\n## iterateOnResult\n`iterateOnResult($query, $callback = null, $emptyCallback = null)`  \n\nTo select multiple rows from a table and doing operations on them use iterateOrResult()  \nPass whatever SELECT query you like and start working on them in the callback  \nData objects returned inside the parameter has two extra properties `numRows` (total number of rows available) and `hasNext` (if has more data in the list)   \nTo do a different operation when the data is empty, pass a empty callback as third parameter (see example below) \n\n`````php\nQuery::iterateOnResult(\n  \"SELECT e.*, a.level FROM employees e\n  LEFT OUTER JOIN authorization a ON a.emp_id=e.id\",\n  function ($dataRow) {\n    // do something with dataRow\n  },\n  function () {\n    // show message like there's no data in the table\n  }\n);\n\n// returns: void\n`````\n\n````sql\n/* query executed: whatever query you wrote */\nSELECT e.*,\n       a.level\nFROM employees e\nLEFT OUTER JOIN\nAUTHORIZATION a ON a.emp_id=e.id\n````\n\n## truncate\n`truncate($tableName)`  \n\nTo truncate (remove all rows) from a table, use truncate()\n\n`````php\nQuery::truncate(\"users\");\n\n// returns: boolean\n`````\n\n````sql\n/* query executed */\nTRUNCATE TABLE users;\n````\n\n## upsert\n`upsert($tableName, $dataObject, $whereString)`  \n\nTo update or insert a row in the table, use upsert(). It internally calls the `insert()` if row doesn't exists else, it calls the `updateWhere()`.\n\n`````php\nQuery::upsert(\"users\", [\"name\"=\u003e\"Abdul\"], \"`name`='John'\");\n\n// returns: boolean || integer (inserted row id)\n`````\n\n````sql\n/* query executed: update */\nINSERT INTO users (\"name\")\nVALUES (\"Abdul\")\n````\n\n````sql\n/* query executed: insert */\nUPDATE TABLE users\nSET `name`='Abdul'\nWHERE `name`='John'\n````\n\n## Features\n\n - Reliable\n - Lightweight\n - Easy to use and implement\n - Great code readability\n - Highly customizable\n\n## Contribution\nWant to contribute? Great fork this repo and create a pull request if you\n- found any error\n- want to add a new function\n- upgraded an existing function\n\n## Credits\n- [KNEX.JS](https://knexjs.org/) (Inspiration)\n- SQL Formatter: https://sqlformat.org/","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futsavdotpro%2Fphpqueryutils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Futsavdotpro%2Fphpqueryutils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futsavdotpro%2Fphpqueryutils/lists"}