{"id":20300986,"url":"https://github.com/sujanshresthanet/pdo-wrapper","last_synced_at":"2026-05-09T11:42:59.813Z","repository":{"id":144426740,"uuid":"500932123","full_name":"sujanshresthanet/pdo-wrapper","owner":"sujanshresthanet","description":"Pdo Wrapper","archived":false,"fork":false,"pushed_at":"2022-06-07T17:12:21.000Z","size":23,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-14T10:49:01.722Z","etag":null,"topics":["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/sujanshresthanet.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-07T17:04:22.000Z","updated_at":"2022-06-07T17:13:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"16b1c68d-00a5-48b0-80c8-06875a215086","html_url":"https://github.com/sujanshresthanet/pdo-wrapper","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/sujanshresthanet%2Fpdo-wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sujanshresthanet%2Fpdo-wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sujanshresthanet%2Fpdo-wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sujanshresthanet%2Fpdo-wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sujanshresthanet","download_url":"https://codeload.github.com/sujanshresthanet/pdo-wrapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241795910,"owners_count":20021555,"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":["mysql","pdo","php"],"created_at":"2024-11-14T16:23:01.835Z","updated_at":"2026-05-09T11:42:59.737Z","avatar_url":"https://github.com/sujanshresthanet.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](http://poser.pugx.org/sujanshresthanet/pdo-wrapper/v)](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [![Total Downloads](http://poser.pugx.org/sujanshresthanet/pdo-wrapper/downloads)](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [![Latest Unstable Version](http://poser.pugx.org/sujanshresthanet/pdo-wrapper/v/unstable)](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [![License](http://poser.pugx.org/sujanshresthanet/pdo-wrapper/license)](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [![PHP Version Require](http://poser.pugx.org/sujanshresthanet/pdo-wrapper/require/php)](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [![Dependents](http://poser.pugx.org/sujanshresthanet/pdo-wrapper/dependents)](https://packagist.org/packages/sujanshresthanet/pdo-wrapper)\n\nPDO Database Class\n============================\n\n#### 1. Add via composer :\n```\ncomposer require sujanshresthanet/pdo-wrapper\n```\n#### 2. Extend your class db and your object model :\n\n```php\n#Create class MyDb for example and extends from \\sujanshresthanet\\pdo\\Db.\n#To use the class\n#if you want object model from table mysql you can use like these following class Persons with extends from Crud\n\nrequire(__DIR__ . './config/database.php');\n\nclass MyDb extends \\sujanshresthanet\\pdo\\Db {\n\n    public function __construct() {\n        parent::__construct(HOST, DB_NAME, DB_USER, DB_PASS);\n    }\n}\n\n//create class Persons if we want\nclass Persons extends sujanshresthanet\\pdo\\Crud {\n\n    protected $table = 'persons';\n\n    # Primary Key of the table\n    protected $pk = 'id';\n\n    public $className = 'MyDb';\n\n}\n```\n\n#### 3. Bootstraping in your project example in index.php\n```php\n\u003c?php\nrequire(__DIR__ . '/vendor/autoload.php');\ninclude 'MyDb.php';\n$db = new MyDb;\n\n$persons = $db-\u003equery(\"SELECT * FROM persons\");\necho \"\u003cpre\u003e\";print_r($persons);\n\n$person = new Persons();\n// Create new person\n$person-\u003eFirstname = \"Josh\";\n$person-\u003eAge = \"20\";\n$person-\u003eSex = \"F\";\n$created = $person-\u003ecreate();\n\n//  Or give the bindings to the constructor\n$person = new Persons(array(\"Firstname\" =\u003e \"Josh\", \"age\" =\u003e \"20\", \"sex\" =\u003e \"F\"));\n$created = $person-\u003ecreate();\n\n```\n\n#### 4.  Logs - Modify the read/write rights of the root folder\n\nEverytime an exception is thrown by the database class a log file gets created or modified.\nThese logs are stored in the logs directory. Which means the database class needs write access for the logs folder.\nIf the files are on a webserver you'll have to modify the rights of the root folder otherwise you'll get a \"Permission denied\" error.\n\nThe log file is a simple plain text file with the current date('year-month-day') as filename.\n\n## Examples\nBelow some examples of the basic functions of the database class. I've included a SQL dump so you can easily test the database\nclass functions.\n#### The persons table\n| id | firstname | lastname | sex | age\n|:-----------:|:------------:|:------------:|:------------:|:------------:|\n| 1       |        John |     Doe    | M | 19\n| 2       |        Bob  |     Black    | M | 41\n| 3       |        Zoe  |     Chan    | F | 20\n| 4       |        Josh |     Martin    | M | 14\n| 5       |        Adam|     Rose    | M | 56\n\n#### Fetching everything from the table\n```php\n\u003c?php\n// Fetch whole table\n$persons = $db-\u003equery(\"SELECT * FROM persons\");\n```\n#### Fetching with Bindings (ANTI-SQL-INJECTION):\nBinding parameters is the best way to prevent SQL injection. The class prepares your SQL query and binds the parameters\nafterwards.\n\nThere are three different ways to bind parameters.\n```php\n\u003c?php\n// 1. Read friendly method\n$db-\u003ebind(\"id\",\"1\");\n$db-\u003ebind(\"firstname\",\"John\");\n$person   =  $db-\u003equery(\"SELECT * FROM Persons WHERE firstname = :firstname AND id = :id\");\n\n// 2. Bind more parameters\n$db-\u003ebindMore(array(\"firstname\"=\u003e\"John\",\"id\"=\u003e\"1\"));\n$person   =  $db-\u003equery(\"SELECT * FROM Persons WHERE firstname = :firstname AND id = :id\"));\n\n// 3. Or just give the parameters to the method\n$person   =  $db-\u003equery(\"SELECT * FROM Persons WHERE firstname = :firstname\",array(\"firstname\"=\u003e\"John\",\"id\"=\u003e\"1\"));\n```\n\nMore about SQL injection prevention : http://indieteq.com/index/readmore/how-to-prevent-sql-injection-in-php\n\n#### Fetching Row:\nThis method always returns only 1 row.\n```php\n\u003c?php\n// Fetch a row\n$ages     =  $db-\u003erow(\"SELECT * FROM Persons WHERE  id = :id\", array(\"id\"=\u003e\"1\"));\n```\n##### Result\n| id | firstname | lastname | sex | age\n|:-----------:|:------------:|:------------:|:------------:|:------------:|\n| 1       |        John |     Doe    | M | 19\n#### Fetching Single Value:\nThis method returns only one single value of a record.\n```php\n\u003c?php\n// Fetch one single value\n$db-\u003ebind(\"id\",\"3\");\n$firstname = $db-\u003esingle(\"SELECT firstname FROM Persons WHERE id = :id\");\n```\n##### Result\n|firstname\n|:------------:\n| Zoe\n#### Fetching Column:\n```php\n\u003c?php\n// Fetch a column\n$names    =  $db-\u003ecolumn(\"SELECT Firstname FROM Persons\");\n```\n##### Result\n|firstname |\n|:-----------:\n|        John\n|        Bob\n|        Zoe\n|        Josh\n|        Adam\n### Delete / Update / Insert\nWhen executing the delete, update, or insert statement by using the query method the affected rows will be returned.\n```php\n\u003c?php\n\n// Delete\n$delete   =  $db-\u003equery(\"DELETE FROM Persons WHERE Id = :id\", array(\"id\"=\u003e\"1\"));\n\n// Update\n$update   =  $db-\u003equery(\"UPDATE Persons SET firstname = :f WHERE Id = :id\", array(\"f\"=\u003e\"Jan\",\"id\"=\u003e\"32\"));\n\n// Insert\n$insert   =  $db-\u003equery(\"INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)\", array(\"f\"=\u003e\"Tyler\",\"age\"=\u003e\"20\"));\n\n// Do something with the data\nif($insert \u003e 0 ) {\n  return 'Succesfully created a new person !';\n}\n\n```\n## Method parameters\nEvery method which executes a query has the optional parameter called bindings.\n\nThe \u003ci\u003erow\u003c/i\u003e and the \u003ci\u003equery\u003c/i\u003e method have a third optional parameter  which is the fetch style.\nThe default fetch style is \u003ci\u003ePDO::FETCH_ASSOC\u003c/i\u003e which returns an associative array.\n\nHere an example :\n\n```php\n\u003c?php\n  // Fetch style as third parameter\n  $person_num =     $db-\u003erow(\"SELECT * FROM Persons WHERE id = :id\", array(\"id\"=\u003e\"1\"), PDO::FETCH_NUM);\n\n  print_r($person_num);\n  // Array ( [0] =\u003e 1 [1] =\u003e Johny [2] =\u003e Doe [3] =\u003e M [4] =\u003e 19 )\n\n```\nMore info about the PDO fetchstyle : http://php.net/manual/en/pdostatement.fetch.php\n\n\nEasyCRUD\n============================\nThe easyCRUD is a class which you can use to easily execute basic SQL operations like(insert, update, select, delete) on your database.\nIt uses the database class I've created to execute the SQL queries.\n\nActually it's just a little ORM class.\n\n## How to use easyCRUD\n#### 1. First, create a new class. Then require the easyCRUD class.\n#### 2. Extend your class to the base class Crud and add the following fields to the class.\n#### Example class :\n```php\n\u003c?php\nrequire_once(\"easyCRUD.class.php\");\n\nclass YourClass  Extends Crud {\n\n  # The table you want to perform the database actions on\n  protected $table = 'persons';\n\n  # Primary Key of the table\n  protected $pk  = 'id';\n\n}\n```\n\n## EasyCRUD in action.\n\n#### Creating a new person\n```php\n\u003c?php\n// First we\"ll have create the instance of the class\n$person = new person();\n\n// Create new person\n$person-\u003eFirstname  = \"Josh\";\n$person-\u003eAge        = \"20\";\n$person-\u003eSex        = \"F\";\n$created            = $person-\u003eCreate();\n\n//  Or give the bindings to the constructor\n$person  = new person(array(\"Firstname\"=\u003e\"Josh\",\"age\"=\u003e\"20\",\"sex\"=\u003e\"F\"));\n$created = $person-\u003eCreate();\n\n// SQL Equivalent\n\"INSERT INTO persons (Firstname,Age,Sex) VALUES ('Josh','20','F')\"\n```\n#### Deleting a person\n```php\n\u003c?php\n// Delete person\n$person-\u003eId  = \"17\";\n$deleted     = $person-\u003eDelete();\n\n// Shorthand method, give id as parameter\n$deleted     = $person-\u003eDelete(17);\n\n// SQL Equivalent\n\"DELETE FROM persons WHERE Id = 17 LIMIT 1\"\n```\n#### Saving person's data\n```php\n\u003c?php\n// Update personal data\n$person-\u003eFirstname = \"John\";\n$person-\u003eAge  = \"20\";\n$person-\u003eSex = \"F\";\n$person-\u003eId  = \"4\";\n// Returns affected rows\n$saved = $person-\u003eSave();\n\n//  Or give the bindings to the constructor\n$person = new person(array(\"Firstname\"=\u003e\"John\",\"age\"=\u003e\"20\",\"sex\"=\u003e\"F\",\"Id\"=\u003e\"4\"));\n$saved = $person-\u003eSave();\n\n// SQL Equivalent\n\"UPDATE persons SET Firstname = 'John',Age = 20, Sex = 'F' WHERE Id= 4\"\n```\n#### Finding a person\n```php\n\u003c?php\n// Find person\n$person-\u003eId = \"1\";\n$person-\u003eFind();\n\necho $person-\u003efirstname;\n// Johny\n\n// Shorthand method, give id as parameter\n$person-\u003eFind(1);\n\n// SQL Equivalent\n\"SELECT * FROM persons WHERE Id = 1\"\n```\n#### Getting all the persons\n```php\n\u003c?php\n// Finding all person\n$persons = $person-\u003eall();\n\n// SQL Equivalent\n\"SELECT * FROM persons\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsujanshresthanet%2Fpdo-wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsujanshresthanet%2Fpdo-wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsujanshresthanet%2Fpdo-wrapper/lists"}