PDO-MySQL:在准备好的语句绑定中,布尔值被转换为1或空字符串 [英] PDO-MySQL: Boolean values get converted to 1 or empty string on prepared statement binding

查看:86
本文介绍了PDO-MySQL:在准备好的语句绑定中,布尔值被转换为1或空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些布尔值插入JSON类型的列中.

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]);

这将导致执行以下SQL.

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')

是否有一种方法可以使PDO在SQL语句中将其转换为TRUEFALSE,然后将其转换为正确的JSON-布尔值.

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.

我现在尝试使用bindParambindValue代替execute的参数.那些不能解决问题,因为PDO仍将布尔值转换为01.

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.

似乎有一个 11岁的错误报告,仍然没有解决.

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.

这篇关于PDO-MySQL:在准备好的语句绑定中,布尔值被转换为1或空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆