{"id":16877657,"url":"https://github.com/nikic/db","last_synced_at":"2025-03-17T06:31:27.425Z","repository":{"id":950306,"uuid":"730575","full_name":"nikic/DB","owner":"nikic","description":"Very simple and  secure PDO wrapper class","archived":false,"fork":false,"pushed_at":"2019-10-17T18:49:11.000Z","size":23,"stargazers_count":93,"open_issues_count":0,"forks_count":23,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-27T19:02:02.424Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nikic.png","metadata":{"files":{"readme":"README.markdown","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":"2010-06-20T14:42:42.000Z","updated_at":"2025-01-22T20:13:45.000Z","dependencies_parsed_at":"2022-07-05T23:31:04.861Z","dependency_job_id":null,"html_url":"https://github.com/nikic/DB","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/nikic%2FDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2FDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2FDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2FDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nikic","download_url":"https://codeload.github.com/nikic/DB/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243847062,"owners_count":20357317,"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":"2024-10-13T15:44:43.669Z","updated_at":"2025-03-17T06:31:27.023Z","avatar_url":"https://github.com/nikic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple database wrapper for PDO\n\nThis is a *very* simplistic database wrapper for PDO, with two goals:\n**Simplicity** and **Security**!\n\n## First design goal: Simple!\n\nI did not use the Singleton pattern for this class, because Singletons\nalways involve unnecessarily much code and aren't that nice to use and read.\nA typical query execution of a Singleton-based DB-class looks like this:\n\n```php\n$db = DB::getInstance();\n$db-\u003equery('SELECT ...');\n$db-\u003eexec('INSERT INTO ...');\n```\n\nOr, if it's only one query:\n\n```php\nDB::getInstance()-\u003equery('SELECT ...');\n```\n\nNow, I think this `getInstance()-\u003e` part of the code neither carries\nfurther information, nor is useful in some way. Therefore, I simply left\nthis part out, resulting in:\n\n```php\nDB::query('SELECT ...');\nDB::exec('SELECT ...');\n```\n\nMuch nicer, isn't it?\n\nSo, wonder which static methods you can use? All! All methods PDO implements.\nAll calls to static methods are simply redirected to the PDO instance.\n\n## Second design goal: Secure!\n\nApart from this redirecting functionality this class offers two further methods:\n`DB::q()` and `DB::x()`. These methods are shortcuts to `DB::query()` and `DB::exec()`\nwith the difference of \"auto quoting\":\n\n```php\nDB::q(\n\t'SELECT * FROM user WHERE group = ?s AND points \u003e ?i',\n\t'user', 7000 //                   ^^              ^^\n)\n```\n\nSee those question marks? These are placeholders, which will be replaced with the arguments\npassed after the query. There are several types of placeholders:\n\n- `?s` (string)  inserts the argument applying string escaping through `PDO-\u003equote`\n- `?i` (integer) inserts the argument applying integer escaping through `intval`\n- `?a` (array)   inserts the argument, converting it to a list of string-escaped values:\n     DB::q('SELECT * FROM user WHERE name IN ?a', array('foo', 'bar', 'hello', 'world'));\n     // results in:\n     // SELECT * FROM user WHERE name IN ('foo','bar','hello','world')\n\n## Configuration\n\nThere are two versions of this class available, one for PHP 5.3\n(DB.php) and one for PHP 5.2 (DB_forPHP52.php). The only difference\nis, that the former uses `__callStatic` to redirect the static calls\nto the PDO instance, the latter simply redefines all methods. (You may\nobviously use the 5.2 version on PHP 5.3, it actually should be slightly\nfaster.)\n\nSo, to get going and use this class, you have to modify the\n`DB::instance` method, which by default is defined like this:\n\n```php\npublic static function instance() {\n\tif (self::$instance === null) {\n\t\tself::$instance = new PDO(\n\t\t\t'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME,\n\t\t\tDB_USER,\n\t\t\tDB_PASS\n\t\t);\n\t\tself::$instance-\u003esetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\t}\n\n\treturn self::$instance;\n}\n```\n\nReplace the arguments of `new PDO()` to satisfy your needs.\nTen, `require_once` the file and have fun using it!\n\n## Short reference\n\n```php\nclass DB\n{\n\t// returns the database instance\n\tpublic static function instance()\n\n\t// DB::query with autoQuote\n\tpublic static function q($query, $params, $...)\n\n\t// DB::exec with autoQuote\n\tpublic static function x($query, $params, $...)\n\n\t// autoQuote as described above\n\tpublic static function autoQuote($query, array $args)\n\n    // All methods defined by PDO\n    // e.d prepare(), quote(), ...\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikic%2Fdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikic%2Fdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikic%2Fdb/lists"}