我在 mysql 数据库中有包含序列化数据的列(使用 PHP JSON 编码来序列化它们)。
这是一个例子:
{"name":"Group ltd","email":"support@domain.org","auth":"Andrey Ucholnik"}
mysql 中是否有一个内置函数可以在没有 PHP 的情况下提取这些值? 我的意思是构建将反序列化数据的查询。
当然可以结合使用 LOCATE 和 SUBSTR 函数来做到这一点,但如果可能的话,我更喜欢内置的东西。
最佳答案
MySQL 中没有内置函数,但通过使用 PHP 中的小代码,您可以轻松地完成以下操作:
<?php
$json = '{{"generated": "2010-02-26T22:26:03.156866 blahblahblah ": null, "thumbnail_url": "http://thumbs.mochiads.com/c/g/tetword-pro/_thumb_100x100.jpg", "screen4_url": "http://thumbs.mochiads.com/c/g/tetword-pro/screen4.png", "leaderboard_enabled": true, "resolution": "600x550", "width": 600}]}}';
$out = json_decode($json, true);
foreach($out["games"] as $game) {
$name = addslashes($game[name]);
$description = addslashes($game[description]);
//Here you can use your mysql_insert code
mysql_query("INSERT INTO games (name, description) VALUES('$name', '$description')") or die (mysql_error());
}
?>
关于mysql - 如何从存储在序列化 JSON 列中的 Mysql 列中提取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5094340/