{"id":21023517,"url":"https://github.com/jahidulpabelislam/database","last_synced_at":"2025-12-25T18:49:23.910Z","repository":{"id":42939591,"uuid":"235673806","full_name":"jahidulpabelislam/database","owner":"jahidulpabelislam","description":"Simple wrapper for PDO.","archived":false,"fork":false,"pushed_at":"2024-04-13T11:13:22.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"2.x","last_synced_at":"2024-04-14T12:01:30.527Z","etag":null,"topics":["database","mysql","pdo","php"],"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}},"created_at":"2020-01-22T21:46:12.000Z","updated_at":"2024-04-15T23:37:11.909Z","dependencies_parsed_at":"2024-04-15T23:37:05.731Z","dependency_job_id":"04b816be-fe97-4c10-9d8b-96762c4a6486","html_url":"https://github.com/jahidulpabelislam/database","commit_stats":{"total_commits":30,"total_committers":3,"mean_commits":10.0,"dds":"0.33333333333333337","last_synced_commit":"00343bcffc8a9a069cbd34d50202d1fccc5629a6"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fdatabase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fdatabase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fdatabase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fdatabase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jahidulpabelislam","download_url":"https://codeload.github.com/jahidulpabelislam/database/tar.gz/refs/heads/2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243456563,"owners_count":20293905,"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","pdo","php"],"created_at":"2024-11-19T11:18:29.137Z","updated_at":"2025-12-25T18:49:23.903Z","avatar_url":"https://github.com/jahidulpabelislam.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Database\n\n[![CodeFactor](https://www.codefactor.io/repository/github/jahidulpabelislam/database/badge)](https://www.codefactor.io/repository/github/jahidulpabelislam/database)\n[![Latest Stable Version](https://poser.pugx.org/jpi/database/v/stable)](https://packagist.org/packages/jpi/database)\n[![Total Downloads](https://poser.pugx.org/jpi/database/downloads)](https://packagist.org/packages/jpi/database)\n[![Latest Unstable Version](https://poser.pugx.org/jpi/database/v/unstable)](https://packagist.org/packages/jpi/database)\n[![License](https://poser.pugx.org/jpi/database/license)](https://packagist.org/packages/jpi/database)\n![GitHub last commit (branch)](https://img.shields.io/github/last-commit/jahidulpabelislam/database/2.x.svg?label=last%20activity)\n\nSimple extension to PDO with some extra convenient methods.\n\n## Installation\n\nUse [Composer](https://getcomposer.org/)\n\n```bash\n$ composer require jpi/database \n```\n\n## Usage\n\n### Initialisation\n\nFirst, create an instance of the Database class by providing PDO connection parameters:\n\n```php\n$connection = new \\JPI\\Database(\n    \"mysql:host=localhost;dbname=your_database\",\n    \"username\",\n    \"password\"\n);\n```\n\n### Available Methods\n\nExtra Methods:\n- `prep(string, array): PDOStatement`: when you want to bind some parameters to a query\n- `run(string, array): PDOStatement`: when you bind some parameters to a query and want to execute it\n- `selectAll(string, array): array`: for a `SELECT` query, returns a multidimensional array of all the rows found\n- `selectFirst(string, array): array|null`: for a `SELECT` query that has `LIMIT 1`, returns an associative array of the first row found (if any)\n- `getLastInsertedId(): int|null`: helpful after an `INSERT` query, returns the ID of the newly inserted row\n\nOverridden Methods:\n- `exec(string, array): int`: for `INSERT`, `UPDATE` and `DELETE` queries, returns the number of rows affected\n\nAll methods except `getLastInsertedId` take the query as the first parameter (required), and an array of params to bind to the query (optional).\n\n### Examples:\n\n(Assuming instance has been created and set to a variable named `$connection`)\n\n#### prep:\n\n```php\n// Prepare a statement with bound parameters (without executing)\n$statement = $connection-\u003eprep(\n    \"SELECT * FROM users WHERE email = :email;\",\n    [\"email\" =\u003e \"jahidul@jahidulpabelislam.com\"]\n);\n\n// You can now execute it later\n$statement-\u003eexecute();\n```\n\n#### run:\n\n```php\n// Prepare and execute a query in one step\n$statement = $connection-\u003erun(\n    \"SELECT * FROM users WHERE email = :email;\",\n    [\"email\" =\u003e \"jahidul@jahidulpabelislam.com\"]\n);\n\n// Fetch results from the statement\n$users = $statement-\u003efetchAll(PDO::FETCH_ASSOC);\n```\n\n#### selectAll:\n\n```php\n$rows = $connection-\u003eselectAll(\"SELECT * FROM users;\");\n\n/**\n$rows = [\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\n#### selectFirst:\n\n```php\n$row = $connection-\u003eselectFirst(\"SELECT * FROM users LIMIT 1;\");\n\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\n#### exec:\n\n```php\n// INSERT\n$numberOfRowsAffected = $connection-\u003eexec(\n    \"INSERT INTO users (first_name, last_name, email, password) VALUES (:first_name, :last_name, :email, :password);\",\n    [\n        \"first_name\" =\u003e \"Jahidul\",\n        \"last_name\" =\u003e \"Islam\",\n        \"email\" =\u003e \"jahidul@jahidulpabelislam.com\",\n        \"password\" =\u003e \"password\",\n    ]\n);\n\n// UPDATE\n$numberOfRowsAffected = $connection-\u003eexec(\n    \"UPDATE users SET first_name = :first_name WHERE id = :id;\",\n    [\n        \"id\" =\u003e 1,\n        \"first_name\" =\u003e \"Pabel\",\n    ]\n);\n\n// DELETE\n$numberOfRowsAffected = $connection-\u003eexec(\"DELETE FROM users WHERE id = :id;\", [\"id\" =\u003e 1]);\n```\n\n#### getLastInsertedId:\n\n```php\n// INSERT a new user\n$connection-\u003eexec(\n    \"INSERT INTO users (first_name, last_name, email, password) VALUES (:first_name, :last_name, :email, :password);\",\n    [\n        \"first_name\" =\u003e \"Jahidul\",\n        \"last_name\" =\u003e \"Islam\",\n        \"email\" =\u003e \"jahidul@jahidulpabelislam.com\",\n        \"password\" =\u003e \"password\",\n    ]\n);\n\n// Get the ID of the newly inserted row\n$newUserId = $connection-\u003egetLastInsertedId();\n// $newUserId = 3\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 a [issue](https://github.com/jahidulpabelislam/database/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%2Fdatabase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjahidulpabelislam%2Fdatabase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjahidulpabelislam%2Fdatabase/lists"}