{"id":16326796,"url":"https://github.com/jr-cologne/db-class","last_synced_at":"2026-04-28T18:07:46.108Z","repository":{"id":57002121,"uuid":"87917039","full_name":"jr-cologne/db-class","owner":"jr-cologne","description":"A simple database class with PHP, PDO and a query builder.","archived":false,"fork":false,"pushed_at":"2018-08-26T12:49:03.000Z","size":83,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-14T22:12:34.539Z","etag":null,"topics":["database","db-class","pdo","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jr-cologne.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-11T09:43:20.000Z","updated_at":"2022-03-21T10:40:29.000Z","dependencies_parsed_at":"2022-08-21T14:10:44.347Z","dependency_job_id":null,"html_url":"https://github.com/jr-cologne/db-class","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jr-cologne%2Fdb-class","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jr-cologne%2Fdb-class/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jr-cologne%2Fdb-class/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jr-cologne%2Fdb-class/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jr-cologne","download_url":"https://codeload.github.com/jr-cologne/db-class/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254235712,"owners_count":22036966,"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","db-class","pdo","php","query-builder"],"created_at":"2024-10-10T23:09:35.194Z","updated_at":"2026-04-28T18:07:41.069Z","avatar_url":"https://github.com/jr-cologne.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# db-class\n\n[![Build Status](https://travis-ci.org/jr-cologne/db-class.svg?branch=master)](https://travis-ci.org/jr-cologne/db-class)\n\nThis project is a simple database class with PHP, PDO and a query builder.\n\nThe class extends PDO for more control and in order to keep all features of PDO.\n\n## Requirements\n\n- [PHP](http://php.net) (version 7.0 or higher)\n- Database, which supports PDO (e.g. MySQL)\n\n## Installation\n\nIf you want to use the database class for your own project, you have two options to install it:\n\n### Using Composer (recommended)\n\nOnce you have installed [Composer](https://getcomposer.org/), execute this command:\n\n```\ncomposer require jr-cologne/db-class\n```\n\nThen you just have to include the autoloader:\n\n```php\nrequire_once 'vendor/autoload.php';\n```\n\n### Manual Installation\n\n1. Download the ZIP file of this project\n2. Unzip it and move everything to your own project directory.\n3. Include all files of the database class into your project like that:\n\n```php\nrequire_once 'path/to/db-class/src/DB.php';\nrequire_once 'path/to/db-class/src/QueryBuilder.php';\nrequire_once 'path/to/db-class/src/Exceptions/UnsupportedKeywordException.php';\n```\n\nNow you should be ready to start!\n\n## Basic Usage\n\n### Namespace\n\nBefore instantiating the class, always make sure to use the right namespaces:\n\n```php\nuse JRCologne\\Utils\\Database\\DB;\nuse JRCologne\\Utils\\Database\\QueryBuilder;\n```\n\n### Instantiating Class\n\nTo be able to use the class, you have to instantiate it.\n\nJust do this:\n\n```php\n$db = new DB(new QueryBuilder);\n```\n\n### Connecting to Database\n\nYou can connect to a database with the help of the method `DB::connect()`.\n\nAn simple example:\n\n```php\nif ($db-\u003econnect('mysql:host=localhost;dbname=db-class-example;charset=utf8', 'root', 'root')) {\n  echo 'Successfully connected to database';\n} else {\n  echo 'Connection failed';\n}\n```\n\n### Checking Connection to Database\n\nYou can also check the connection to the database by the method `DB::connected()` after connecting.\n\nExample:\n\n```php\nif ($db-\u003econnected()) {\n  echo 'Successfully connected to database';\n} else {\n  echo 'Connection failed';\n}\n```\n\n### Retrieving Data from Database\n\nIn order to retrieve data from a database, you need to walk through the following three steps:\n\n1. Choose a table with the method `DB::table()`.\n2. Select the data you want to retrieve.\n3. Retrieve the selected data.\n\nFortunately, this is super simple with the database class:\n\n```php\n$data = $db-\u003etable('users')-\u003eselect('*')-\u003eretrieve();\n\nif ($data === false) {\n  echo 'Ops, something went wrong retrieving the data from the database!\u003cbr\u003e';\n} else if (empty($data)) {\n  echo 'It looks like there is no data in the database!\u003cbr\u003e';\n} else {\n  echo 'Successfully retrieved the data from the database!\u003cbr\u003e';\n\n  echo '\u003cpre\u003e', print_r($data, true), '\u003c/pre\u003e';\n}\n```\n\nIt will basically retrieve all records from the selected table.\n\n### Inserting Data into Database\n\nIf you want to insert data into a database, you have two methods which you can use:\n\n- `DB::insert()` (to insert one row of data)\n- `DB::multi_insert()` (to insert multiple rows of data)\n\nIn this case, we are just going to insert one row.\n\nThe procedure is as follows:\n\n1. Choose a table with the method `DB::table()`.\n2. Insert the data with the method `DB::insert()`.\n\nExample:\n\n```php\n$inserted = $db-\u003etable('users')-\u003einsert('username, password', [\n  'username' =\u003e 'test',\n  'password' =\u003e 'password'\n]);\n\nif ($inserted) {\n  echo 'Data has successfully been inserted';\n} else if ($inserted === 0) {\n  echo 'Ops, some data could not be inserted';\n} else {\n  echo 'Inserting of data is failed';\n}\n```\n\n### Updating Data from Database\n\nIn case you want to update data from a database, you can use the method `DB::update()`.\n\nThe following steps are required:\n\n1. Choose a table with the method `DB::table()`.\n2. Update the data with the method `DB::update()`.\n\nExample:\n\n```php\nif (\n  $db-\u003etable('users')-\u003eupdate(\n    [\n      'username' =\u003e 'test123',  // new data\n      'password' =\u003e 'password123',\n    ],\n    [\n      'username' =\u003e 'test',    // where clause\n      'password' =\u003e 'password',\n    ]\n  )\n) {\n  echo 'Data has successfully been updated';\n} else {\n  echo 'Updating data failed';\n}\n```\n\nThis will update the record(s) where the `username` is equal to `test` and the `password` is equal to `password` to `test123` for the `username` and `password123` for the `password`.\n\n### Deleting Data from Database\n\nIn order to delete data from a database, follow these steps:\n\n1. Choose a table with the method `DB::table()`.\n2. Delete the data with the method `DB::delete()`.\n\nHere's an simple example which deletes the record(s) where the `username` is equal to `test`:\n\n```php\nif ($db-\u003etable('users')-\u003edelete([\n  'username' =\u003e 'test'  // where clause\n])) {\n  echo 'Data has successfully been deleted';\n} else {\n  echo 'Deleting data failed';\n}\n```\n\n### Custom Where Clauses\n\n#### Custom Logical Operators in Where Clause\n\nSince the release of [version 2.3](https://github.com/jr-cologne/db-class/releases/tag/v2.3.0), a where clause can also have custom logical operators.\n\nThis is how a where clause with custom logical operators could look like when retrieving data from a database:\n\n```php\n$data = $db-\u003etable('users')-\u003eselect('*', [\n  'id' =\u003e 1,\n  '||',\n  'username' =\u003e 'test'\n])-\u003eretrieve();\n```\n\n#### Custom Comparison Operators in Where Clause\n\nSince the release of [version 2.4](https://github.com/jr-cologne/db-class/releases/tag/v2.4.0), a where clause can also have custom comparison operators.\n\nThis is how a where clause with custom comparison operators could look like when retrieving data from a database:\n\n```php\n$data = $db-\u003etable('users')-\u003eselect('*', [\n  [\n    'id',\n    '\u003e=',\n    1\n  ],\n  'username' =\u003e 'test',\n  [\n    'password',\n    '!=',\n    'test123'\n  ]\n])-\u003eretrieve();\n```\n\n### Using PDO's functionality\n\nSince the database class is extending PDO, you can use the whole functionality of PDO with this class as well.\n\nJust connect to the database using the method `DB::connect()` and after that simply use everything as normal.\n\nAn quick example:\n\n```php\n// include all files\nrequire_once('vendor/autoload.php');\n\n// use right namespaces\nuse JRCologne\\Utils\\Database\\DB;\nuse JRCologne\\Utils\\Database\\QueryBuilder;\n\n// instantiate database class with query builder\n$db = new DB(new QueryBuilder);\n\n// connect to database\n$db-\u003econnect('mysql:host=localhost;dbname=db-class-example;charset=utf8', 'root', 'root');\n\n// prepare query like with PDO class\n$stmt = $db-\u003eprepare(\"SELECT * FROM `users`\");\n\n// execute query\n$stmt-\u003eexecute();\n\n// fetch all results\n$results = $stmt-\u003efetchAll();\n```\n\n### API\n\nLooking for a complete overview of each class, property and method of this database class?\n\nJust head over to the [`API.md`](https://github.com/jr-cologne/db-class/blob/master/src/API.md) file where you can find everything you need.\n\nIt is located in the source (`src`) folder.\n\n## Further Examples / Stuff for Testing\n\nYou want to see further examples of using the database class or you just want to play around with it a little bit?\n\n- You can find further examples in the file [`example/example.php`](https://github.com/jr-cologne/db-class/blob/master/example/example.php).\n- To play around with the database class, you can use the database provided in the file [`example/db-class-example.sql`](https://github.com/jr-cologne/db-class/blob/master/example/db-class-example.sql). Just import it in your database client and you are ready to start!\n\n## Contributing\n\nFeel free to contribute to this project! Any kind of contribution is highly appreciated.\n\nIn case you have any questions regarding your contribution, do not hesitate to open an Issue.\n\n## Versioning\n\nThis project is using the rules of semantic versioning (since version 2). For more information, visit [semver.org](http://semver.org/).\n\n## License\n\nThis project is licensed under the [MIT License](https://github.com/jr-cologne/db-class/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjr-cologne%2Fdb-class","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjr-cologne%2Fdb-class","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjr-cologne%2Fdb-class/lists"}