简体   繁体   English

在 mySQL 中插入带有特殊字符的 JSON 编码值失败

[英]Inserting in mySQL a JSON encoded value with special chars fails

I am trying to execute this query with PDO:我正在尝试使用 PDO 执行此查询:

UPDATE categories
SET keywords = '{"bg":["keyword1","\u0431\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438 "]}'
WHERE id = 4;

The problem is that it seems to record the hex value without the backslashes ("\\").问题是它似乎记录了没有反斜杠(“\\”)的十六进制值。 It does show up without the backslashes in my app but also in PHPMyAdmin.它确实在我的应用程序和 PHPMyAdmin 中没有反斜杠显示。

I use exec() on this query, and I have tried it with this full-text query, without any dynamic/transformed variables.我在这个查询上使用了 exec(),我已经用这个全文查询试过了,没有任何动态/转换的变量。 The PDO parameters are as follow: PDO参数如下:

PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"

Can someone explain me what is happening here?有人可以解释我这里发生了什么吗? Thanks!谢谢!

UPDATE: I have executed this exact query from mysql's CLI, then displayed it there, and the problem is the same: all the backslashes have disappeared.更新:我已经从 mysql 的 CLI 执行了这个确切的查询,然后在那里显示它,问题是一样的:所有的反斜杠都消失了。 So it doesn't seem the problem has anything to do with PDO, but with MySQL - or me!因此,问题似乎与 PDO 无关,而是与 MySQL 或我有关! My version is 5.1.53我的版本是5.1.53

The \\ character has special meaning in SQL. \\字符在 SQL 中具有特殊含义。 Use bound parameters instead of hardcoding the value into the base string you use for the query.使用绑定参数而不是将值硬编码到用于查询的基本字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM