How to save php object in MySQL table?

I installed a table that has only one field for a BLOB (binary large object), but when I try to insert it into the table, it gives an error message in which it was unable to convert the object to a string. This is my request:

mysql_query("INSERT INTO objects (inquery) VALUES($inquery)"); 
+8
object php mysql
source share
2 answers

Serialize it:

 $str = serialize($object); 

If your object contains private / protected fields, it is also a good idea for base64_encode() serialized object, since these properties will lead to the use of ascii-1 characters, which will break when editing a column manually, for example. with phpMyAdmin ..

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

+20
source share

use json_encode to encode the object before storing it in Mysql and then json_decode to decode the object

+2
source share

All Articles