如何在MySQL中更新序列化数据 [英] How to update serialize data in MySQL

查看:233
本文介绍了如何在MySQL中更新序列化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mySQL数据库中有此序列化数据

I have this serialize data in my mySQL database

a:4:{i:0;s:7:"bvl.png";i:1;s:8:"ccop.jpg";i:2;s:11:"reyborn.png";i:3;s:13:"swopgroup.jpg";}

如何更新此数据,例如要删除ccop.jpg?

How can I update this data, for example I want to delete ccop.jpg?

推荐答案

您必须从数据库中获取值,对其进行反序列化,删除该元素,对其进行序列化,然后再次保存.

You have to fetch the value from the database, unserialize it, remove the element, serialize it and save again.

$remove = "ccop.jpg";

//
// get the string from the database
//

$arr = unserialize($str);

foreach($arr as $key => $value)
  if ($value == $remove) unset($arr[$key]);

$str = serialize($arr);

//
// save the string back to the database
//


与其保存序列化的值列表,不如使用一个规范化数据库,然后执行一个简单的DELETE FROM images WHERE object_id = ....


Instead of saving serialized list of values, it's better to have a normalized database and just do a simple DELETE FROM images WHERE object_id = ....

这篇关于如何在MySQL中更新序列化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆