{"id":26796418,"url":"https://github.com/peterujah/db-controller","last_synced_at":"2025-04-22T19:57:52.626Z","repository":{"id":57038174,"uuid":"440667968","full_name":"peterujah/db-controller","owner":"peterujah","description":"Php PDO wrapper","archived":false,"fork":false,"pushed_at":"2023-10-22T10:46:18.000Z","size":43,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T18:18:35.720Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/peterujah.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":"2021-12-21T22:34:11.000Z","updated_at":"2023-09-24T03:47:22.000Z","dependencies_parsed_at":"2022-08-23T23:30:54.141Z","dependency_job_id":null,"html_url":"https://github.com/peterujah/db-controller","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterujah%2Fdb-controller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterujah%2Fdb-controller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterujah%2Fdb-controller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterujah%2Fdb-controller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peterujah","download_url":"https://codeload.github.com/peterujah/db-controller/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250315961,"owners_count":21410473,"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":[],"created_at":"2025-03-29T18:18:38.271Z","updated_at":"2025-04-22T19:57:52.606Z","avatar_url":"https://github.com/peterujah.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DBController\n\nDBController is a PHP PDO wrapper that provides a convenient way to interact with a database using the PDO extension.\n\n## Installation\n\nYou can install the package via Composer by running the following command:\n\n```bash\ncomposer require peterujah/db-controller\n```\n\n# USAGES\n\nTo use DBController, follow these easy steps.\n\n1. Create an instance of the DBController class by passing the database configuration as an array or a path to a configuration file that returns array.\n\n\n```php\nuse Peterujah\\NanoBlock\\DBController;\n// Pass the configuration as an array\n$config = [\n    'VERSION' =\u003e 'mysql',\n    'HOST' =\u003e 'localhost',\n    'PORT' =\u003e 3306,\n    'NAME' =\u003e 'my_database',\n    'USERNAME' =\u003e 'root',\n    'PASSWORD' =\u003e 'password',\n];\n\n$handler = new DBController($config);\n```\n\nOr extend `\\Peterujah\\NanoBlock\\DBController` to set your connection details like below\n\n```php\nclass Conn extends \\Peterujah\\NanoBlock\\DBController{ \n\tpublic function __construct(bool $development = false){\n \t\t$config = array(\n\t\t\t\"PORT\" =\u003e 3306,\n\t\t\t\"HOST\" =\u003e \"localhost\",\n\t\t\t\"VERSION\" =\u003e \"mysql\",\n\t\t);\n\t\tif($development){\n\t\t\t$config[\"USERNAME\"] = \"root\";\n\t\t\t$config[\"PASSWORD\"] = \"\";\n\t\t\t$config[\"NAME\"] = \"dbname\";\n\t\t}else{\n\t\t\t$config[\"USERNAME\"] = \"dbusername\";\n\t\t\t$config[\"PASSWORD\"] = \"dbpass\";\n\t\t\t$config[\"NAME\"] = \"dbname\";\n\t\t} \n\t\t$this-\u003eonDebug = $development;\n\t\tparent::__construct($config);\n\t}\n}\n```\nInitialize your custom class\n```php \n$handler = new Conn($_SERVER[\"HOST_NAME\"]==\"localhost\");\n```\n\nNow run query select, insert, update, delete etc.. using prepare statment\n\n```php\n$handler-\u003eprepare('SELECT * FROM users WHERE username = :username LIMIT 1');\n$handler-\u003ebind(':username', \"Peter\");\n$handler-\u003eexecute();\n$res = $handler-\u003egetOne();\n$handler-\u003efree();\n```\n\nOr run query select, insert, update, delete etc.. using query\n\n```php\n$handler-\u003equery('SELECT * FROM users');\n$res = $handler-\u003egetAll();\n$handler-\u003efree();\n```\n\n# Customization\n\nCustomize the configuration or enable debugging as needed.\n\n```php\n// Set a configuration value\n$handler-\u003esetConfig('VERSION', 'pgsql');\n\n// Enable debugging mode\n$handler-\u003esetDebug(true);\n\n```\n\n# Error Handling\n\nDBController provides error handling for database operations. You can retrieve the error information using the `error()` or `errorInfo()` methods.\n\n```php\n// Get the error information for the last statement execution\n$errorInfo = $handler-\u003eerror();\n\n// Print the error message\nif ($errorInfo !== null) {\n    echo \"Error: \" . $errorInfo[2];\n}\n```\n\n# Debugging\n\nYou can enable debugging mode to get more detailed information about the executed statements by calling the `dumpDebug()` method.\n\n```php\n// Enable debugging mode\n$handler-\u003esetDebug(true);\n\n// Dump the debug information for the last statement execution\n$handler-\u003edumpDebug();\n\n```\n\n# Methods\n\nUse the various methods provided by the DBController class to interact with the database.\n\n```php\n// Prepare a statement\n$query = 'SELECT * FROM users WHERE id = :id';\n$handler-\u003eprepare($query);\n\n// Bind values to parameters\n$handler-\u003ebind(':id', 1);\n\n//Binds a variable to a parameter.\n$handler-\u003eparam(':id', 1, DBController::_INT)\n\n// Execute the statement\n$handler-\u003eexecute();\n\n// Fetch a single row as an object\n$user = $handler-\u003egetOne();\n\n// Fetch all rows as an array of objects\n$users = $handler-\u003egetAll();\n\n// Get the number of rows affected by the last statement execution\n$rowCount = $handler-\u003erowCount();\n\n// Get the last inserted ID\n$lastInsertId = $handler-\u003egetLastInsertId();\n\n// Free up the statement cursor\n$handler-\u003efree();\n```\n\n| Options         | Description                                                                         |\n|-----------------|-------------------------------------------------------------------------------------|\n| prepare(string)            | Call \"prepare\" with sql query string to prepare query execution                                                   |\n| query(string)            | Call \"query\" width sql query without \"bind\" and \"param\"                                                  |\n| bind(param, value, type)          | Call \"bind\" to bind value to the pdo prepare method                                  |\n| param(param, value, type)           | Call \"param\" to bind parameter to the pdo statment                                    |\n| execute()           | Execute prepare statment                                       |\n| rowCount()           | Get result row count                                      |\n| getOne()           | Get one resault row, this is useful when you set LIMIT 1                                       |\n| getAll()           | Retrieve all result                                      |\n| getInt()           | Gets integer useful when you select COUNT()                                      |\n| getAllObject()          | Gets result object                                       |\n| getLastInsertedId()           | Gets list inserted id from table                                      |\n| free()           | Free database connection                                       |\n| dumpDebug()           | Dump debug sql query parameter                                      |\n| errorInfo()           | Print PDO prepare statment error when debug is enabled                                     |\n| error()           | Print connection or execution error when debug is enabled                                     |\n| setDebug(bool)           | Sets debug status                                       |\n| setConfig(array)           | Sets connection config array                                       |\n| conn()           | Retrieve DBController Instance useful when you call \"setConfig(config)\"                                    |\n\n\n# Configuration format\n\nConnection config array example \n\n```php \n[\n     PORT =\u003e 3306,\n     HOST =\u003e \"localhost\",\n     VERSION =\u003e \"mysql\",\n     NAME =\u003e \"dbname\",\n     USERNAME =\u003e \"root\",\n     PASSWORD =\u003e \"\"\n]\n```\n\n# Contributing\n\nContributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request.\n\n# License\n\n```bash\nDBController is open-source software licensed under the MIT license.\n```\n\n\n\t\t\t\t\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeterujah%2Fdb-controller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeterujah%2Fdb-controller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeterujah%2Fdb-controller/lists"}