I insert json data to mySql database like this:
{ "socialnetwork": { "twitter": "tw-u" }, "information": { "country": "Cuba", "state": "Sancti Spiritus" }, "settings": { "social-enable": "1" } }
Now, I try to update social-enable
value using JSON_REPLACED
method like this:
$builder = $this->db->table('users'); $builder->where('id', 2)->set('information', "JSON_REPLACE(information, '$.settings.social-enable', '0')", false) ; $builder->update();
In action I see this error:
Invalid JSON path expression. The error is around character position 27.
How do can I fix this problem?
Advertisement
Answer
… Names of keys must be double-quoted strings or valid ECMAScript identifiers (see Identifier Names and Identifiers, in the ECMAScript Language Specification). …
Try:
... JSON_REPLACE(information, '$.settings."social-enable"', '0') ...
UPDATE
Try:
$builder->where('id', 2)->set('information', "JSON_REPLACE(information, '$.settings."social-enable"', '0')", false);