简体   繁体   中英

Inserting in mySQL a JSON encoded value with special chars fails

I am trying to execute this query with 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.

I use exec() on this query, and I have tried it with this full-text query, without any dynamic/transformed variables. The PDO parameters are as follow:

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. So it doesn't seem the problem has anything to do with PDO, but with MySQL - or me! My version is 5.1.53

The \\ character has special meaning in SQL. Use bound parameters instead of hardcoding the value into the base string you use for the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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