I'm trying to insert some boolean values into JSON-type columns.
$taskSql = "INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', :title, 'done', :done), :taskListId)";
$taskStatement = $connection->prepare($taskSql);
$taskStatement->execute([":title" => $task->title, ":done" => $task->done, ":taskListId" => $id]);
Which results in following SQL being executed.
-- $task->done is false
INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', 'New Task', 'done', ''), '12')
-- $task->done is true
INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', 'New Task', 'done', '1'), '12')
Is there a way to make PDO turn those into TRUE
or FALSE
in the SQL-statement, which would then convert them into proper JSON-boolean-values.
EDIT:
I've now tried using bindParam
and bindValue
instead of the argument to execute
. Those don't fix the problem as PDO still converts the boolean values to 0
or 1
.
EDIT: Looks like there is an 11 year old bug report, that still hasn't been addressed.
I endet up changing the insert line to
INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', :title, 'done', :done = TRUE || :done = '1'), :taskListId);
Not a solution but a workaround.
来源:https://stackoverflow.com/questions/45412196/pdo-mysql-boolean-values-get-converted-to-1-or-empty-string-on-prepared-stateme