{"id":20208293,"url":"https://github.com/cubiclesoft/csdb","last_synced_at":"2025-10-11T22:16:17.389Z","repository":{"id":36923414,"uuid":"41230563","full_name":"cubiclesoft/csdb","owner":"cubiclesoft","description":"A portable, cross-platform, cross-database, lightweight, debuggable, replication-aware, migration-friendly, transaction-capable, data access layer (DAL) for PHP.","archived":false,"fork":false,"pushed_at":"2021-07-05T21:58:55.000Z","size":80,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-04T11:23:33.769Z","etag":null,"topics":["cross-database","cross-platform","csdb","dal","data-access-layer"],"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/cubiclesoft.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":"2015-08-23T01:16:57.000Z","updated_at":"2024-11-26T17:26:13.000Z","dependencies_parsed_at":"2022-09-07T18:00:34.272Z","dependency_job_id":null,"html_url":"https://github.com/cubiclesoft/csdb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cubiclesoft/csdb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubiclesoft%2Fcsdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubiclesoft%2Fcsdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubiclesoft%2Fcsdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubiclesoft%2Fcsdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cubiclesoft","download_url":"https://codeload.github.com/cubiclesoft/csdb/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubiclesoft%2Fcsdb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276328899,"owners_count":25623323,"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","status":"online","status_checked_at":"2025-09-21T02:00:07.055Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["cross-database","cross-platform","csdb","dal","data-access-layer"],"created_at":"2024-11-14T05:34:58.361Z","updated_at":"2025-09-22T00:31:22.510Z","avatar_url":"https://github.com/cubiclesoft.png","language":"PHP","readme":"CSDB Data Access Layer\n======================\n\nA PHP library that provides a portable, cross-platform, cross-database, lightweight, debuggable, replication-aware, migration-friendly, transaction-capable, [data access layer](http://en.wikipedia.org/wiki/Data_access_layer) (DAL) for PHP.\n\nA collection of useful, lightweight classes intended to be dropped into projects that need database access.  CSDB wraps the PHP [PDO](http://www.php.net/manual/en/book.pdo.php) layer with useful functionality for cross-database application development.  Minimal dependencies allows for only including the files needed for a project.\n\n[![Donate](https://cubiclesoft.com/res/donate-shield.png)](https://cubiclesoft.com/donate/) [![Discord](https://img.shields.io/discord/777282089980526602?label=chat\u0026logo=discord)](https://cubiclesoft.com/product-support/github/)\n\nFeatures\n--------\n\n* Cross-platform.  Runs under PHP 5.3 and later for all OSes that PHP supports.\n* Cross-database capable.  The same application code can generate two completely different queries for two different database products but achieve the same result.  Quite useful for writing and maintaining open source software products that talk to a database.\n* Lightweight.  The base class plus one derived class instantiation plus an active database connection only uses an additional 300KB to 570KB RAM (depends on the database).\n* Debuggable.  Easily see the output of each query, its parameter values, query number, how long the query took, and total time for all queries for the current connection.\n* Replication-aware.  Automatically switches to a replication master when running change queries (INSERT, UPDATE, DELETE, etc).\n* Migration-friendly.  The full versions of each database class allow for quickly migrating both tables and data from one database product to another.\n* Nested transaction support.  Allows for nested BeginTransaction() and Commit() calls.\n* Full support for:  MySQL/Maria DB, PostgreSQL, SQLite.\n* Beta support for:  Oracle (OCI).\n* Has a liberal open source license.  MIT or LGPL, your choice.\n* Designed for relatively painless integration into your project.\n* Sits on GitHub for all of that pull request and issue tracker goodness to easily submit changes and ideas respectively.\n\nBasic Usage\n-----------\n\nConnecting to a database:\n\n```php\n\u003c?php\n\t// Replace '_mysql' references below with the correct database class and use the relevant Connect() call.\n\trequire_once \"support/db.php\";\n\trequire_once \"support/db_mysql.php\";\n\n\ttry\n\t{\n\t\t$db = new CSDB_mysql();\n\n\t\t// Enable debug mode for testing only.\n//\t\t$db-\u003eSetDebug(fopen(\"out.txt\", \"wb\"));\n\n\t\t// MySQL/Maria DB.\n\t\t$db-\u003eConnect(\"mysql:host=127.0.0.1\", \"username\", \"*******\");\n//\t\t$db-\u003eSetMaster(\"mysql:host=remotehost\", \"username\", \"*******\");\n\n\t\t// Postgres.\n\t\t$db-\u003eConnect(\"pgsql:host=127.0.0.1\", \"username\", \"*******\");\n//\t\t$db-\u003eSetMaster(\"pgsql:host=remotehost\", \"username\", \"*******\");\n\n\t\t// SQLite.\n\t\t$db-\u003eConnect(\"sqlite:/var/path/to/sqlite.db\");\n\n\t\t// Oracle.\n\t\t$db-\u003eConnect(\"oci:dbname=//localhost/ORCL\", \"username\", \"*******\");\n//\t\t$db-\u003eSetMaster(\"oci:dbname=//remotehost/ORCL\", \"username\", \"*******\");\n\n\t\t// Assumes 'testdb' exists.\n\t\t$db-\u003eQuery(\"USE\", \"testdb\");\n\t}\n\tcatch (Exception $e)\n\t{\n\t\techo \"Unable to connect to the database.  \" . htmlspecialchars($e-\u003egetMessage()) . \"\\n\";\n\t\texit();\n\t}\n?\u003e\n```\n\nStandard queries:\n\n```php\n\u003c?php\n\ttry\n\t{\n\t\t// SELECT multiple rows.\n\t\t$result = $db-\u003eQuery(\"SELECT\", array(\n\t\t\t\"*\",\n\t\t\t\"FROM\" =\u003e \"?\",\n\t\t\t\"WHERE\" =\u003e \"created \u003e= ?\",\n\t\t\t\"ORDER BY\" =\u003e \"id DESC\",\n\t\t\t\"LIMIT\" =\u003e 100\n\t\t), \"app_users\", CSDB::ConvertToDBTime(time() - 3 * 24 * 60 * 60));\n\n\t\twhile ($row = $result-\u003eNextRow())\n\t\t{\n\t\t\techo $row-\u003eid . \" | \" . $row-\u003euser . \" | \" . $row-\u003eemail . \" | \" . date(\"M, j Y @ H:i\", CSDB::ConvertFromDBTime($row-\u003ecreated)) . \"\\n\";\n\t\t}\n\n\t\t// SELECT one row.\n\t\t$row = $db-\u003eGetRow(\"SELECT\", array(\n\t\t\t\"*\",\n\t\t\t\"FROM\" =\u003e \"?\",\n\t\t\t\"WHERE\" =\u003e \"user = ?\",\n\t\t), \"app_users\", $_REQUEST[\"username\"]);\n\n\t\t// SELECT the first column of the first row.\n\t\t$row = (int)$db-\u003eGetOne(\"SELECT\", array(\n\t\t\t\"COUNT(*) AS c\",\n\t\t\t\"FROM\" =\u003e \"?\",\n\t\t), \"app_users\");\n\n\t\t// Transaction support.\n\t\t$db-\u003eBeginTransaction();\n\n\t\t// INSERT row.\n\t\t$db-\u003eQuery(\"INSERT\", array(\"app_users\", array(\n\t\t\t\"user\" =\u003e $_REQUEST[\"username\"],\n\t\t\t\"email\" =\u003e $_REQUEST[\"email\"],\n\t\t\t\"created\" =\u003e CSDB::ConvertToDBTime(time()),\n\t\t\t\"updated\" =\u003e CSDB::ConvertToDBTime(time()),\n\t\t), \"AUTO INCREMENT\" =\u003e \"id\"));\n\n\t\t$id = $db-\u003eGetInsertID();\n\n\t\t// UPDATE row.\n\t\t$db-\u003eQuery(\"UPDATE\", array(\"app_users\", array(\n\t\t\t\"email\" =\u003e $_REQUEST[\"email\"],\n\t\t\t\"updated\" =\u003e CSDB::ConvertToDBTime(time()),\n\t\t), \"WHERE\" =\u003e \"id = ?\"), $id);\n\n\t\t// DELETE row.\n\t\t$db-\u003eQuery(\"DELETE\", array(\"app_users\", \"WHERE\" =\u003e \"id = ?\"), $id);\n\n\t\t// Finalize transaction.\n\t\t$db-\u003eCommit();\n\t}\n\tcatch (Exception $e)\n\t{\n\t\t// Rollback transaction.\n\t\t$db-\u003eRollback();\n\n\t\techo \"An error occurred while running a database query.  \" . htmlspecialchars($e-\u003egetMessage()) . \"\\n\";\n\t\texit();\n\t}\n?\u003e\n```\n\nCreating a database and tables (e.g. for an installer):\n\n```php\n\u003c?php\n\t// Create/Use the database.\n\ttry\n\t{\n\t\t$db-\u003eQuery(\"USE\", \"testdb\");\n\t}\n\tcatch (Exception $e)\n\t{\n\t\ttry\n\t\t{\n\t\t\t$db-\u003eQuery(\"CREATE DATABASE\", array(\"testdb\", \"CHARACTER SET\" =\u003e \"utf8\", \"COLLATE\" =\u003e \"utf8_general_ci\"));\n\t\t\t$db-\u003eQuery(\"USE\", \"testdb\");\n\t\t}\n\t\tcatch (Exception $e)\n\t\t{\n\t\t\techo \"Unable to create/use database 'testdb'.  \" . htmlspecialchars($e-\u003egetMessage()) . \"\\n\";\n\t\t\texit();\n\t\t}\n\t}\n\n\t// Create missing database tables.\n\ttry\n\t{\n\t\t$appusersfound = $db-\u003eTableExists(\"app_users\");\n\t}\n\tcatch (Exception $e)\n\t{\n\t\techo \"Unable to determine the existence of a database table.  \" . htmlspecialchars($e-\u003egetMessage()) . \"\\n\";\n\t\texit();\n\t}\n\n\tif (!$appusersfound)\n\t{\n\t\ttry\n\t\t{\n\t\t\t$db-\u003eQuery(\"CREATE TABLE\", array(\"app_users\", array(\n\t\t\t\t\"id\" =\u003e array(\"INTEGER\", 8, \"UNSIGNED\" =\u003e true, \"NOT NULL\" =\u003e true, \"PRIMARY KEY\" =\u003e true, \"AUTO INCREMENT\" =\u003e true),\n\t\t\t\t\"user\" =\u003e array(\"STRING\", 1, 255, \"NOT NULL\" =\u003e true),\n\t\t\t\t\"email\" =\u003e array(\"STRING\", 1, 255, \"NOT NULL\" =\u003e true),\n\t\t\t\t\"created\" =\u003e array(\"DATETIME\", \"NOT NULL\" =\u003e true),\n\t\t\t\t\"updated\" =\u003e array(\"DATETIME\", \"NOT NULL\" =\u003e true),\n\t\t\t),\n\t\t\tarray(\n\t\t\t\tarray(\"UNIQUE\", array(\"user\"), \"NAME\" =\u003e \"app_users_user\"),\n\t\t\t\tarray(\"UNIQUE\", array(\"email\"), \"NAME\" =\u003e \"app_users_email\"),\n\t\t\t\tarray(\"KEY\", array(\"created\"), \"NAME\" =\u003e \"app_users_created\"),\n\t\t\t)));\n\t\t}\n\t\tcatch (Exception $e)\n\t\t{\n\t\t\techo \"Unable to create the database table 'app_users'.  \" . htmlspecialchars($e-\u003egetMessage()) . \"\\n\";\n\t\t\texit();\n\t\t}\n\t}\n?\u003e\n```\n\nRemember that PHP function and method calls are case-insensitive.  So it's fine if you prefer `connect()`, `query()`, `nextRow()`, `getRow()`, etc.  Just be consistent.\n\nMore Information\n----------------\n\nFull documentation and examples can be found in the 'docs' directory of this repository.\n\n* [Queries](https://github.com/cubiclesoft/csdb/blob/master/docs/csdb_queries.md) - Documentation for all query types including SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, etc.\n* [CSDB class](https://github.com/cubiclesoft/csdb/blob/master/docs/csdb.md) - Documentation for the CSDB base class.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcubiclesoft%2Fcsdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcubiclesoft%2Fcsdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcubiclesoft%2Fcsdb/lists"}