PDO-MySQL: Boolean values get converted to 1 or empty string on prepared statement binding

若如初见. 提交于 2019-12-02 05:14:21

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!