11

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)"); 

2 Answers 2

28

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

Sign up to request clarification or add additional context in comments.

4 Comments

So when I go to grab it from the table after it has been stored, how do i convert it back to an object?
$object = unserialize($str);
be careful though, not all php objects can be serialized. resources (db connects, http connections, etc) cannot be. see php.net/manual/en/function.serialize.php
@contagious no worries, it is a dead simple object i created myself.
2

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

2 Comments

When json_decoding the object will be a stdClass Object
sorry for the late response, but you can add the true to the second param something like this json_decode($json, true);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.