如何在MySQL表中存储php对象? [英] How do I store an php object in a MySQL table?

查看:141
本文介绍了如何在MySQL表中存储php对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个表,该表只有一个字段用于BLOB(二进制大对象),但是当我尝试将其插入表中时,会引发错误,表明它无法将对象转换为字符串.这是我的查询:

I have set up a table that has only one field for a BLOB (Binary large object) but when I try to Insert it into the table it throws an error stating that it failed to convert the object to a string. This is my Query:

mysql_query("INSERT INTO objects (inquery) VALUES($inquery)");

推荐答案

序列化它:

$str = serialize($object);

如果您的对象包含私有/受保护的字段,base64_encode()序列化的对象也是个好主意,因为这些属性将导致使用ascii-1字符,这些字符在手动编辑列时会中断,例如使用phpMyAdmin..

If your object contains private/protected fields it's also a good idea to base64_encode() the serialized object as those properties will result in ascii-1 characters being used which would break when editing the column manually e.g. with phpMyAdmin..

要恢复对象,只需 unserialize() 字符串(如果需要,可以先base64_decode()).

To restore your object, you simply unserialize() the string (base64_decode() it before if necessary).

这篇关于如何在MySQL表中存储php对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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