{"id":16327255,"url":"https://github.com/ferg1e/pajamasql","last_synced_at":"2026-04-16T10:01:55.214Z","repository":{"id":17983007,"uuid":"20987737","full_name":"ferg1e/pajamaSQL","owner":"ferg1e","description":"PHP SQL wrapper and model layer","archived":false,"fork":false,"pushed_at":"2019-10-16T15:46:00.000Z","size":98,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-18T02:51:27.699Z","etag":null,"topics":["database-abstraction-layer","mysql","mysqli-library","php","postgresql","sql","sqlite","sqlite-wrapper"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ferg1e.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-19T04:31:29.000Z","updated_at":"2019-11-22T05:29:07.000Z","dependencies_parsed_at":"2022-09-12T02:11:59.639Z","dependency_job_id":null,"html_url":"https://github.com/ferg1e/pajamaSQL","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/ferg1e/pajamaSQL","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ferg1e%2FpajamaSQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ferg1e%2FpajamaSQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ferg1e%2FpajamaSQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ferg1e%2FpajamaSQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ferg1e","download_url":"https://codeload.github.com/ferg1e/pajamaSQL/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ferg1e%2FpajamaSQL/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31880882,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T09:23:21.276Z","status":"ssl_error","status_checked_at":"2026-04-16T09:23:15.028Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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-abstraction-layer","mysql","mysqli-library","php","postgresql","sql","sqlite","sqlite-wrapper"],"created_at":"2024-10-10T23:10:51.434Z","updated_at":"2026-04-16T10:01:55.195Z","avatar_url":"https://github.com/ferg1e.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pajamaSQL\n\npajamaSQL is a PHP SQL database layer.\nIt supports MySQL, PostgreSQL and SQLite.\n\n## Table of Contents\n\n[Documentation](#documentation)\\\n[Requirements](#requirements)\\\n[Source Code](#source-code)\\\n[Unit Tests](#unit-tests)\\\n[Examples](#examples)\\\n[Installation](#installation)\\\n[Overview](#overview)\\\n[MySQL](#mysql)\\\n[PostgreSQL](#postgresql)\\\n[SQLite](#sqlite)\\\n[Models](#models)\\\n[Error Handling](#error-handling)\\\n[License](#license)\n\n## Documentation\n\nThis [README](https://github.com/al-codepone/pajamaSQL/blob/master/README.md)\nis currently the only documentation.\n\n## Requirements\n\n**PHP 5.6.0+ or 7+**; [Composer](https://getcomposer.org/);\nand at least one of the follow PHP vendor specific database extensions:\n[Mysqli](http://www.php.net/manual/en/book.mysqli.php),\n[PostgreSQL](http://www.php.net/manual/en/book.pgsql.php) and/or\n[SQLite3](http://www.php.net/manual/en/book.sqlite3.php). The unit tests\nneed **PHP 7.2.0+**.\n\n\n## Source Code\n\nThis [project](https://github.com/al-codepone/pajamaSQL)\nand its [source code](https://github.com/al-codepone/pajamaSQL/tree/master/src/pjsql)\nare available on GitHub.\n\n## Unit Tests\n\nThere are unit tests in the\n[tests](https://github.com/al-codepone/pajamaSQL/tree/master/tests) directory.\nYou will need to edit the database credentials at the top of\n`MysqlTest.php` and `PgsqlTest.php`.\n\n## Examples\n\nAll the example code in this README is in the\n[examples](https://github.com/al-codepone/pajamaSQL/tree/master/examples)\ndirectory. The examples directory also contains additional examples that are not\nin this README.\nYou need to run `composer install` in the examples directory before running\nany of the examples.\n\n## Installation\n\nInstall using composer:\n\n```javascript\n{\n    \"require\": {\n        \"pajamasql/pajamasql\": \"1.0.0\",\n    }\n}\n```\n\n## Overview\n\npajamaSQL wraps [Mysqli](http://www.php.net/manual/en/book.mysqli.php),\n[PostgreSQL](http://www.php.net/manual/en/book.pgsql.php) and\n[SQLite3](http://www.php.net/manual/en/book.sqlite3.php)\nprepared statements. These are the available methods: `exec()`, `query()`,\n`rquery()`, `prepare()`, `bexec()`, `bquery()`, `brquery()`, `conn()`,\n`error()` and `esc()`. Basic usage looks like this:\n\n```php\n$db-\u003eexec(\n    'insert into foo values(?), (?)',\n    'bar',\n    'baz');\n\n$data = $db-\u003equery(\n    'select * from foo where rowid \u003e ?',\n    1);\n```\n\nThere is also a model layer. A basic model looks like this:\n\n```php\n\u003c?php\n\nnamespace purple;\n\nclass FooModel extends \\pjsql\\DatabaseAdapter {\n    public function createFoo($name) {\n        $this-\u003eexec(\n            'insert into foo values(?)',\n            $name);\n    }    \n}\n```\n\nModel instantiation and usage:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\n$foo_model = purple\\ModelFactory::get('purple\\FooModel');\n\n$foo_model-\u003ecreateFoo('fred');\n```\n\n## MySQL\n\nUse `Mysql()` to connect to a database:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\n$db = new pjsql\\Mysql(\n    'host',\n    'username',\n    'password',\n    'database');\n\necho $db-\u003econn()-\u003estat();\n```\n\n`conn()` above is a [mysqli](https://www.php.net/manual/en/class.mysqli.php) object.\n\nExecute queries with `exec()` and `query()`:\n\n```php\n$db-\u003eexec('create table tanimal(\n    animal_id int auto_increment primary key,\n    name varchar(32))');\n\n$db-\u003eexec('insert into tanimal(name) values(\"tiger\")');\n\n$data = $db-\u003equery('select * from tanimal');\n\nprint_r($data);\n```\n\n`exec()` and `query()` can take query parameters:\n\n```php\n$db-\u003eexec(\n    'insert into tanimal(name) values(?), (?)',\n    'tiger',\n    'eagle');\n\n$data = $db-\u003equery(\n    'select * from tanimal where animal_id = ?',\n    2);\n\nprint_r($data);\n```\n\n`query()` returns an array with all the data.\nIf you want a\n[mysqli_result](https://www.php.net/manual/en/class.mysqli-result.php)\nobject instead then use `rquery()`:\n\n```php\n$result = $db-\u003erquery('select * from tanimal');\n\nwhile($row = $result-\u003efetch_object()) {\n    print_r($row);\n}\n```\n\nUse `prepare()`, `bexec()` and `bquery()` to run a query more than once:\n\n```php\n$stmt = $db-\u003eprepare('insert into tanimal values(null, ?)');\n$db-\u003ebexec($stmt, 'bird');\n$db-\u003ebexec($stmt, 'frog');\n$db-\u003ebexec($stmt, 'cat');\n\n$ids = [1, 2, 3, 4];\n$stmt = $db-\u003eprepare('select name from tanimal where animal_id = ?');\n\nforeach($ids as $id) {\n    $data = $db-\u003ebquery($stmt, $id);\n    print_r($data);\n}\n```\n\n`bquery()` returns an array with all the data.\nIf you want a\n[mysqli_result](https://www.php.net/manual/en/class.mysqli-result.php)\nobject instead then use `brquery()`:\n\n```php\n$floors = [1, 5];\n$stmt = $db-\u003eprepare('select name from tanimal where animal_id \u003e= ?');\n\nforeach($floors as $f) {\n    $result = $db-\u003ebrquery($stmt, $f);\n\n    while($row = $result-\u003efetch_assoc()) {\n        print_r($row);\n    }\n}\n```\n\nWith all the exec and query methods\nyou can put the parameter values into an array and use a types\nstring as the third argument in order to specify the query\nparameter types:\n\n```php\n$db-\u003eexec(\n    'insert into tanimal(name) values(?), (?), (?)',\n    ['lizard', 'cow', 'monkey'],\n    'sss');\n\n$data = $db-\u003equery(\n    'select * from tanimal where animal_id \u003c ?',\n    [3],\n    'i');\n\nprint_r($data);\n```\n\nThe parameter types string above works the same way as the\n[mysqli_stmt::bind_param](https://www.php.net/manual/en/mysqli-stmt.bind-param.php)\n`$types` argument.\n\n## PostgreSQL\n\nUse `Pgsql()` to connect to a database:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\n$db = new pjsql\\Pgsql('dbname=foo user=bar password=baz');\n\necho pg_host($db-\u003econn());\n```\n\n`conn()` above is a PostgreSQL connection resource that is returned\nby [pg_connect()](https://www.php.net/manual/en/function.pg-connect.php).\n\nExecute queries with `exec()` and `query()`:\n\n```php\n$db-\u003eexec('create table tcolor(\n    color_id serial primary key,\n    name varchar(40))');\n\n$db-\u003eexec(\"insert into tcolor(name) values('green')\");\n\n$data = $db-\u003equery('select * from tcolor');\n\nprint_r($data);\n```\n\n`exec()` and `query()` can take query parameters:\n\n```php\n$db-\u003eexec(\n    'insert into tcolor(name) values($1), ($2)',\n    'gold',\n    'silver');\n\n$data = $db-\u003equery(\n    'select * from tcolor where color_id \u003c\u003e $1',\n    1);\n\nprint_r($data);\n```\n\n`query()` returns an array with all the data.\nIf you want a query result resource instead then use `rquery()`:\n\n```php\n$result = $db-\u003erquery('select * from tcolor');\n\nwhile($row = pg_fetch_assoc($result)) {\n    print_r($row);\n}\n```\n\nUse `prepare()`, `bexec()` and `bquery()` to run a query more than once:\n\n```php\n$stmt_name = 'insert1';\n$db-\u003eprepare('insert into tcolor values(default, $1)', $stmt_name);\n$db-\u003ebexec($stmt_name, 'pink');\n$db-\u003ebexec($stmt_name, 'purple');\n$db-\u003ebexec($stmt_name, 'black');\n\n$ids = [1, 2, 3, 4];\n$stmt_name = 'select1';\n$db-\u003eprepare('select name from tcolor where color_id = $1', $stmt_name);\n\nforeach($ids as $id) {\n    $data = $db-\u003ebquery($stmt_name, $id);\n    var_dump($data);\n}\n```\n\n`bquery()` returns an array with all the data.\nIf you want a query result resource instead then use `brquery()`:\n\n```php\n$floors = [4, 3];\n$stmt_name = 'select1';\n$db-\u003eprepare('select * from tcolor where color_id \u003e= $1', $stmt_name);\n\nforeach($floors as $f) {\n    $result = $db-\u003ebrquery($stmt_name, $f);\n\n    while($row = pg_fetch_object($result)) {\n        print_r($row);\n    }\n}\n```\n\n## SQLite\n\nUse `Sqlite()` to connect to a database:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\n$db = new pjsql\\Sqlite('mydb.db');\n\necho get_class($db-\u003econn());\n```\n\n`conn()` above is an [SQLite3](https://www.php.net/manual/en/class.sqlite3.php) object.\n\nExecute queries with `exec()` and `query()`:\n\n```php\n$db-\u003eexec('create table tshape(name text)');\n\n$db-\u003eexec('insert into tshape values(\"circle\")');\n\n$data = $db-\u003equery('select * from tshape');\n\nprint_r($data);\n```\n\n`exec()` and `query()` can take query parameters:\n\n```php\n$db-\u003eexec(\n    'insert into tshape values(?), (?)',\n    'triangle',\n    'square');\n\n$data = $db-\u003equery(\n    'select * from tshape where rowid \u003e ?',\n    1);\n\nprint_r($data);\n```\n\n`query()` returns an array with all the data.\nIf you want an [SQLite3Result](https://www.php.net/manual/en/class.sqlite3result.php)\nobject instead then use `rquery()`:\n\n```php\n$result = $db-\u003erquery('select * from tshape');\n\nwhile($row = $result-\u003efetchArray(SQLITE3_ASSOC)) {\n    print_r($row);\n}\n```\n\nUse `prepare()`, `bexec()` and `bquery()` to run a query more than once:\n\n```php\n$stmt = $db-\u003eprepare('insert into tshape values(?)');\n$db-\u003ebexec($stmt, 'octagon');\n$db-\u003ebexec($stmt, 'oval');\n$db-\u003ebexec($stmt, 'circle');\n\n$ids = [1, 2, 3, 4];\n$stmt = $db-\u003eprepare('select name from tshape where rowid = ?');\n\nforeach($ids as $id) {\n    $data = $db-\u003ebquery($stmt, $id);\n    print_r($data);\n}\n```\n\n`bquery()` returns an array with all the data.\nIf you want an [SQLite3Result](https://www.php.net/manual/en/class.sqlite3result.php)\nobject instead then use `brquery()`:\n\n```php\n$stmt = $db-\u003eprepare('select * from tshape where rowid \u003e= ?');\n$floors = [4, 3, 4];\n\nforeach($floors as $f) {\n    $result = $db-\u003ebrquery($stmt, $f);\n\n    while($row = $result-\u003efetchArray()) {\n        print_r($row);\n    }\n}\n```\n\nWith all the exec and query methods\nyou can put the parameter values into an array and use a types\nstring as the third argument in order to specify the query\nparameter types:\n\n```php\n$db-\u003eexec(\n    'insert into tshape values(?), (?)',\n    ['square', 'circle'],\n    'tt');\n\n$data = $db-\u003equery(\n    'select * from tshape where rowid \u003c ?',\n    [500],\n    'i');\n\nprint_r($data);\n```\n\nThe characters in the above types string use the following mapping:\n\n```\ni = SQLITE3_INTEGER\nf = SQLITE3_FLOAT\nt = SQLITE3_TEXT\nb = SQLITE3_BLOB\nn = SQLITE3_NULL\n```\n\n## Models\n\nSet up a model factory:\n\n```php\n\u003c?php\n\nnamespace purple;\n\nclass ModelFactory extends \\pjsql\\AdapterFactory {\n    protected static function databaseHandle() {\n        return new \\pjsql\\Mysql(\n            'host',\n            'username',\n            'password',\n            'database');\n    }\n}\n```\n\nIf you want to use PostgreSQL or SQLite,\nthen use `Pgsql()` or `Sqlite()` instead of the above `Mysql()`.\n\nCreate a model:\n\n```php\n\u003c?php\n\nnamespace purple;\n\nclass DogModel extends \\pjsql\\DatabaseAdapter {\n    public function install() {\n        $this-\u003eexec('create table tdog(\n            dog_id int auto_increment primary key,\n            name varchar(50))');\n    }\n\n    public function createDog($name) {\n        $this-\u003eexec(\n            'insert into tdog(name) values(?)',\n            $name);\n    }\n\n    public function getDogs() {\n        return $this-\u003equery('select * from tdog');\n    }     \n}\n```\n\nGet a model and call its methods:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\n$dog_model = purple\\ModelFactory::get('purple\\DogModel');\n\n$dog_model-\u003einstall();\n\n$dog_model-\u003ecreateDog('spike');\n$dog_model-\u003ecreateDog('buster');\n\n$data = $dog_model-\u003egetDogs();\n\nprint_r($data);\n```\n\n## Error Handling\n\nWith the exec, query and prepare methods\nyou can handle errors with an exception handler:\n\n```php\nset_exception_handler(function($e) {\n    if($e instanceof pjsql\\DatabaseException) {\n        die($e-\u003egetMessage());\n    }\n    else {\n        throw $e;\n    }\n});\n```\n\nIf you use `conn()`, then call `error()` when\nappropriate(MySQL example):\n\n```php\nif($status = $db-\u003econn()-\u003estat()) {\n    echo $status;\n}\nelse {\n    $db-\u003eerror();\n}\n```\n\n`error()` above will thrown an exception that will be\ncaught by the above exception handler.\n\n## LICENSE\n\nMIT \u003chttp://ryf.mit-license.org/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fferg1e%2Fpajamasql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fferg1e%2Fpajamasql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fferg1e%2Fpajamasql/lists"}