{"id":49880039,"url":"https://github.com/pardnio/php-mysql-cli","last_synced_at":"2026-05-15T13:40:39.485Z","repository":{"id":275483904,"uuid":"926212964","full_name":"pardnio/php-mysql-cli","owner":"pardnio","description":"Lightweight PHP MySQL client with chainable syntax, query builder and read-write separation.","archived":false,"fork":false,"pushed_at":"2025-08-24T08:04:56.000Z","size":30,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-04-24T17:35:29.249Z","etag":null,"topics":["backend","chain-syntax","mysql","php","wrapper-library"],"latest_commit_sha":null,"homepage":"","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/pardnio.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,"zenodo":null}},"created_at":"2025-02-02T20:12:35.000Z","updated_at":"2026-04-05T18:04:30.000Z","dependencies_parsed_at":"2025-05-25T18:46:06.165Z","dependency_job_id":"3c19e250-c032-4c31-93b3-7740efad5cc5","html_url":"https://github.com/pardnio/php-mysql-cli","commit_stats":null,"previous_names":["pardnchiu/phpsql","pardnio/php-sql","pardnltd-tools/php-sql","pardnchiu/php-mysql-pool","pardnchiu/php-mysql-cli","pardnchiu/php-sql","pardnio/php-mysql-cli"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/pardnio/php-mysql-cli","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fphp-mysql-cli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fphp-mysql-cli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fphp-mysql-cli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fphp-mysql-cli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pardnio","download_url":"https://codeload.github.com/pardnio/php-mysql-cli/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fphp-mysql-cli/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33068889,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-15T11:35:32.926Z","status":"ssl_error","status_checked_at":"2026-05-15T11:35:31.362Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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":["backend","chain-syntax","mysql","php","wrapper-library"],"created_at":"2026-05-15T13:40:39.214Z","updated_at":"2026-05-15T13:40:39.465Z","avatar_url":"https://github.com/pardnio.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MySQL CLI\n\n\u003e Lightweight PHP MySQL client with chainable syntax, query builder and read-write separation.\u003cbr\u003e\n\u003e Following stateless architecture principles, providing stable and reliable database operation experience.\n\n[![packagist](https://img.shields.io/packagist/v/pardnchiu/mysql-cli)](https://packagist.org/packages/pardnchiu/mysql-cli)\n[![version](https://img.shields.io/github/v/tag/pardnchiu/php-mysql-cli?label=release)](https://github.com/pardnchiu/php-mysql-cli/releases)\n[![license](https://img.shields.io/github/license/pardnchiu/php-mysql-cli)](LICENSE)\u003cbr\u003e\n[![readme](https://img.shields.io/badge/readme-EN-white)](README.md)\n[![readme](https://img.shields.io/badge/readme-ZH-white)](README.zh.md)\n\n- [Three Core Features](#three-core-features)\n  - [Chainable Syntax](#chainable-syntax)\n  - [Read-Write Separation](#read-write-separation)\n  - [Stable Connection](#stable-connection)\n- [Features](#features)\n- [Usage](#usage)\n  - [Installation](#installation)\n  - [Environment Variables Setup](#environment-variables-setup)\n  - [Basic Usage](#basic-usage)\n- [API Reference](#api-reference)\n  - [Query Builder](#query-builder)\n  - [JOIN Operations](#join-operations)\n  - [Data Operations](#data-operations)\n- [Error Handling](#error-handling)\n- [License](#license)\n- [Author](#author)\n\n## Three Core Features\n\n### Chainable Syntax\nIntuitive query builder syntax that makes complex SQL queries simple and readable with low learning curve\n\n### Read-Write Separation\nAutomatically identifies query types and routes to corresponding database connections, supports read-write separation architecture, effectively distributes database load and improves overall system performance\n\n### Stable Connection\nRetry mechanism automatically handles network jitter and temporary connection failures, ensuring reliability in unstable network environments\n\n## Features\n\n- **Environment Variable Configuration**: Flexible environment variable settings, supports multi-environment deployment\n- **Slow Query Monitoring**: Automatically logs queries over 20ms, assists with performance optimization\n- **Secure Parameter Binding**: Prepared statements prevent SQL injection attacks\n- **Complete CRUD**: Supports full database operations for create, read, update, delete\n- **SQL Function Support**: Built-in common MySQL function recognition and processing\n- **Stateless Design**: Independent cleanup for each request\n\n## Usage\n\n### Installation\n\n```shell\ncomposer require pardnchiu/mysql-cli\n```\n\n### Environment Variables Setup\n\n#### Read Database (Optional)\n```env\nDB_READ_HOST=localhost\nDB_READ_PORT=3306\nDB_READ_USER=read_user\nDB_READ_PASSWORD=read_password\nDB_READ_DATABASE=your_database\nDB_READ_CHARSET=utf8mb4\n```\n\n#### Write Database (Required for write operations)\n```env\nDB_WRITE_HOST=localhost\nDB_WRITE_PORT=3306\nDB_WRITE_USER=write_user\nDB_WRITE_PASSWORD=write_password\nDB_WRITE_DATABASE=your_database\nDB_WRITE_CHARSET=utf8mb4\n```\n\n### Basic Usage\n\n```php\n\u003c?php\n\nuse pardnchiu\\SQL;\n\n// Basic query\n$users = SQL::table(\"users\")\n  -\u003ewhere(\"status\", \"active\")\n  -\u003ewhere(\"age\", \"\u003e\", 18)\n  -\u003eget();\n\n// Complex query with aggregation\n$reports = SQL::table(\"orders\")\n  -\u003eselect(\"user_id\", \"COUNT(*) as order_count\", \"SUM(amount) as total\")\n  -\u003ewhere(\"created_at\", \"\u003e=\", \"2024-01-01\")\n  -\u003egroupBy(\"user_id\")\n  -\u003eorderBy(\"total\", \"DESC\")\n  -\u003elimit(10)\n  -\u003eget();\n```\n\n## API Reference\n\n### Query Builder\n\n- `table($table, $target = \"READ\")` - Set target table and connection type\n  ```php\n  SQL::table(\"users\")           // Read operation (default)\n  SQL::table(\"users\", \"WRITE\")  // Write operation\n  ```\n\n- `select($fields)` - Specify query fields\n  ```php\n  SQL::table(\"users\")-\u003eselect(\"id\", \"name\", \"email\");\n  SQL::table(\"products\")-\u003eselect(\"COUNT(*) as total\");\n  ```\n\n- `where($column, $operator, $value)` - Add conditions\n  ```php\n  // Basic conditions\n  SQL::table(\"users\")-\u003ewhere(\"status\", \"active\");\n  SQL::table(\"orders\")-\u003ewhere(\"amount\", \"\u003e\", 100);\n  \n  // LIKE search (automatically adds wildcards)\n  SQL::table(\"users\")-\u003ewhere(\"name\", \"LIKE\", \"John\");\n  ```\n\n- `orderBy($column, $direction)` - Sorting\n  ```php\n  SQL::table(\"users\")-\u003eorderBy(\"created_at\", \"DESC\");\n  SQL::table(\"products\")-\u003eorderBy(\"price\", \"ASC\");\n  ```\n\n- `limit($count)` / `offset($count)` - Pagination\n  ```php\n  SQL::table(\"users\")-\u003elimit(20)-\u003eoffset(40);\n  ```\n\n### JOIN Operations\n\n```php\n// Inner join\nSQL::table(\"users\")\n  -\u003ejoin(\"profiles\", \"users.id\", \"profiles.user_id\")\n  -\u003eget();\n\n// Left join\nSQL::table(\"users\")\n  -\u003eleftJoin(\"orders\", \"users.id\", \"orders.user_id\")\n  -\u003eselect(\"users.name\", \"COUNT(orders.id) as order_count\")\n  -\u003eget();\n\n// Right join\nSQL::table(\"departments\")\n  -\u003erightJoin(\"employees\", \"departments.id\", \"employees.dept_id\")\n  -\u003eget();\n```\n\n### Data Operations\n\n```php\n// Insert data and get ID\n$userId = SQL::table(\"users\", \"WRITE\")\n  -\u003einsertGetId([\n    \"name\"        =\u003e \"John Doe\",\n    \"email\"       =\u003e \"john@example.com\", \n    \"created_at\"  =\u003e \"NOW()\"\n  ]);\n\n// Update data\n$result = SQL::table(\"users\", \"WRITE\")\n  -\u003ewhere(\"id\", $userId)\n  -\u003eupdate([\n    \"last_login\"  =\u003e \"NOW()\",\n    \"login_count\" =\u003e \"login_count + 1\"\n  ]);\n\n// Raw query\n$customData = SQL::read(\n  \"SELECT u.name, COUNT(o.id) as orders FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.created_at \u003e ? GROUP BY u.id\",\n  [\"2024-01-01\"]\n);\n```\n\n## Error Handling\n\n```php\ntry {\n  $result = SQL::table(\"users\", \"WRITE\")\n    -\u003ewhere(\"id\", 1)\n    -\u003eupdate([\n      \"status\"      =\u003e \"active\", \n      \"updated_at\"  =\u003e \"NOW()\"\n    ]);\n  \n  // Check slow query warnings\n  if (!empty($result[\"info\"])) {\n    error_log(\"Slow query warning: \" . $result[\"info\"]);\n  }\n  \n  echo \"Update successful, affected rows: \" . $result[\"affected_rows\"];\n    \n} catch (\\PDOException $e) {\n  // Database related errors\n  error_log(\"Database error: \" . $e-\u003egetMessage());\n  \n  // Handle based on error code\n  $errorCode = $e-\u003egetCode();\n  if ($errorCode === 2006 || $errorCode === 2013) {\n    // Connection interrupted, system will auto retry\n    echo \"Connection exception, please try again later\";\n  } else {\n    echo \"Database operation failed\";\n  }\n    \n} catch (\\InvalidArgumentException $e) {\n  // Parameter errors\n  error_log(\"Parameter error: \" . $e-\u003egetMessage());\n  echo \"Request parameters are incorrect\";\n    \n} catch (\\Exception $e) {\n  // Other errors\n  error_log(\"System error: \" . $e-\u003egetMessage());\n  echo \"System temporarily unavailable, please contact administrator\";\n}\n```\n\n### Performance Monitoring\n\n```php\n// Enable detailed logging\nerror_reporting(E_ALL);\n\n// Automatically log slow queries (over 20ms)\n$users = SQL::table(\"users\")\n  -\u003ewhere(\"status\", \"active\")\n  -\u003eget();\n\n// Check system logs:\n// [Info] PD\\SQL: [Slow Query: 25.43ms] [SELECT * FROM users WHERE status = ?]\n```\n\n## License\n\nThis project is licensed under [MIT](https://github.com/pardnchiu/mysql-cli/blob/main/LICENSE).\n\n## Author\n\n\u003cimg src=\"https://avatars.githubusercontent.com/u/25631760\" align=\"left\" width=\"96\" height=\"96\" style=\"margin-right: 0.5rem;\"\u003e\n\n\u003ch4 style=\"padding-top: 0\"\u003e邱敬幃 Pardn Chiu\u003c/h4\u003e\n\n\u003ca href=\"mailto:dev@pardn.io\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://pardn.io/image/email.svg\" width=\"48\" height=\"48\"\u003e\n\u003c/a\u003e \u003ca href=\"https://linkedin.com/in/pardnchiu\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://pardn.io/image/linkedin.svg\" width=\"48\" height=\"48\"\u003e\n\u003c/a\u003e\n\n***\n\n©️ 2024 [邱敬幃 Pardn Chiu](https://pardn.io)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpardnio%2Fphp-mysql-cli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpardnio%2Fphp-mysql-cli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpardnio%2Fphp-mysql-cli/lists"}