Skip to content

fix(database-mysql): JSON-encode array bindings before passing to PDO#82

Merged
markshust merged 1 commit into
developfrom
feature/mysql-array-binding-json-encode
May 24, 2026
Merged

fix(database-mysql): JSON-encode array bindings before passing to PDO#82
markshust merged 1 commit into
developfrom
feature/mysql-array-binding-json-encode

Conversation

@markshust
Copy link
Copy Markdown
Collaborator

Closes #81

Summary

PDO silently casts PHP arrays to the string "Array" when bound as query parameters. For JSON-typed columns this either fails the JSON parse or stores the literal string "Array" depending on column type and SQL mode — the same root cause as #71 / #72, but in the MySQL driver.

Fix

Mirrors the database-pgsql approach:

  • Added prepareBindings() helper to MySqlConnection that walks the bindings array and JSON-encodes any array values with JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE.
  • JsonException is caught and converted to a domain ConnectionException::invalidArrayBinding($parameter, $previous) with a clear message identifying the offending parameter.
  • MySqlConnection::query() and MySqlConnection::execute() now route through prepareBindings() before calling $statement->execute().

Tests

  • it JSON-encodes array bindings instead of casting them to the string "Array" — happy path, end-to-end via an SQLite-backed MySqlConnection.
  • it throws ConnectionException when an array binding is not JSON-encodable — failure path using NAN as an array value.

Full suite green: 5147 passing.

Test plan

  • ./vendor/bin/pest packages/database-mysql/tests/ --parallel
  • ./vendor/bin/phpcs clean on touched files
  • ./vendor/bin/php-cs-fixer fix --dry-run clean on touched files
  • composer test

🤖 Generated with Claude Code

PDO silently casts PHP arrays to the string "Array" when bound as query
parameters. For json columns this either fails the JSON parse or stores
the literal string "Array" depending on column type and SQL mode.

Mirrors the database-pgsql fix from #72: pre-process bindings via a
prepareBindings() helper that JSON-encodes any array values with
JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE. JsonException is caught
and converted to a domain ConnectionException::invalidArrayBinding()
that names the offending parameter.

Closes #81

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@markshust markshust merged commit 9d6e9da into develop May 24, 2026
1 check passed
@markshust markshust deleted the feature/mysql-array-binding-json-encode branch May 24, 2026 16:15
@github-actions github-actions Bot added the bug Something isn't working label May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array values bound as literal "Array" string instead of JSON (MySQL)

1 participant