如何使用 JSON_replace MySQL 函数设置 JSON 值?

问题描述 投票:0回答:1

我的代码:

$newElements=json_encode(['name'=>'jorge','age'=>23]);

mysqli_query($db, "UPDATE dl SET data=json_replace(data, '$.list', cast('$newElements' as json)) WHERE id = '$ID' ")

我的错误:

致命错误:未捕获 mysqli_sql_exception:您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在 'json)) WHERE id = '8451'... 附近使用的正确语法

我的清单:

['name' => 'jak', 'age' => 13] let it be: ['name' => 'jorj', 'age' => 21]
    
php mysql mysqli
1个回答
-1
投票
$newElements = json_encode(['name' => 'jorge', 'age' => 23]); // Ensure the new elements are properly escaped for use in the SQL query $newElements = mysqli_real_escape_string($db, $newElements); $query = " UPDATE dl SET data = JSON_REPLACE(data, '$.list', CAST('$newElements' AS JSON)) WHERE id = '$ID' "; mysqli_query($db, $query) or die(mysqli_error($db));
    
© www.soinside.com 2019 - 2024. All rights reserved.