When executing a prepared statement and inserting a jsonb value like '{}'::jsonb
via a bound parameter, I get this error:
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type json
DETAIL: Token "'" is invalid.
CONTEXT: JSON data, line 1: '...
I use the single quote '
to describe a string literal, which I then convert to jsonb
type via 'string literal'::jsonb
. But with prepared statements, it does not work and the abovementioned error is thrown. What am I doing wrong?
The string itself does not have any single quotes in it. The only single quotes are the ones used to wrap the string.
The string itself is the result of encoding a PHP array with using:
json_encode($array, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_TAG)
<?php
$stm = $pdo->prepare("INSERT INTO t (val) VALUES (:val)");
$stm->execute($stm, [':val' => "'{}'::jsonb"]);