开发者

How do I store an php object in a MySQL table?

开发者 https://www.devze.com 2023-02-11 07:05 出处:网络
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开发者_Go百

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开发者_Go百科 string. This is my Query:

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


Serialize it:

$str = serialize($object);

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..

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


use json_encode to encode the object before you save it in Mysql then json_decode to decode the object

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号