【问题标题】:PHP mysql json_setPHP mysql json_set
【发布时间】:2017-07-06 12:42:22
【问题描述】:

我遇到以下问题:我有一个 mysql 数据库 (5.7)。我想更新一个 json-data-entry。

这是我在数据库中的数据的样子:

{
    "version": {
        "0": {
            "value": "1.9.6.81"
        }
    }
}

当我尝试直接从命令行使用 SQL 更新此字段时,没有问题:

update item set elements = JSON_SET(elements, '$."version"."0"."value"', "1.9.8.40") where name like 'Client XZY';

但是当我尝试使用 PHP 时,我遇到了混合引号和双引号的一些问题。

$connection = new mysqli('localhost', 'user', 'password');    
$updateSql = "update item set elements = JSON_SET(elements, '$."version'.'0'.'value"', '1.9.8.20') where name like 'Client XYZ';';

if($connection->query($updateSql) === TRUE){
        echo 'good :) ';
}
else{
        echo 'bad :( ';
}

【问题讨论】:

  • 只需在您也用作文字分隔符的任何引号之前添加一个反斜杠。即,如果您的 PHP 字符串文字以 " 开头,则需要将该字符串中的每个 " 写为 \"。
  • 提供你的输入元素,比如版本是可变的?
  • 用这个更新字符串试过了,没有成功$updateSql = "update item set elements = JSON_SET(elements, '$.\"version'.\"0\".\"value\"', '1.9.8.20') where name like 'Client XYZ';";

标签: php mysql sql json set


【解决方案1】:

搞定了

$connection = new mysqli('localhost', 'root', 'xxxxx');
mysqli_select_db($connection,"MYSQL5");

$updateSql = "UPDATE item SET elements = JSON_SET(elements, '$.\"version\".\"0\".\"value\"', '1.9.8.40') WHERE name LIKE 'lient XYZ';";

if($connection->query($updateSql) === TRUE){
        echo 'good :)';
}
else{
        printf("Errormessage: %s\n", $connection->error);
}

【讨论】:

    猜你喜欢
    • 2019-07-04
    • 2016-11-13
    • 2020-01-02
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多