Original answer:
The TEXT field in MySQL stores up to 65535 bytes, so I assume it is truncated there.
Use MEDIUMTEXT or LONGTEXT instead.
In addition to this, there are potential problems with how you get data into and out of the database. PHP serialized strings can contain null bytes (byte 0), and it looks like it is not being transmitted properly.
One way around this is to encode the string through something like base64_encode() , which uses a very convenient alphanumeric alphabet / character. This will solve your problems if you increase the BLOB type to MEDIUMBLOB or LONGBLOB .
However, if you send your queries correctly to the database, you can safely send the original string. Since you are using Kohana, here is a sample that works great for me.
Short version:
$sql = 'INSERT INTO serialized_object (data) VALUES (:data)'; DB::query(Database::INSERT, $sql)-> param(':data', $serialization)-> execute();
the code:
<?php class Article {} class Word {} class Controller_Welcome extends Controller { public function action_index() { $object = unserialize(hex2bin(file_get_contents('/tmp/data.hex'))); $serialization = serialize($object); $sql = 'INSERT INTO serialized_object (data) VALUES (:data)'; DB::query(Database::INSERT, $sql)-> param(':data', $serialization)-> execute(); $saved_length = DB::query(Database::SELECT, ' SELECT LENGTH(data) AS l FROM serialized_object ORDER BY id DESC LIMIT 1 ')->execute()->get('l'); if ($saved_length != strlen($serialization)) { throw new Exception("Database length is incorrect. Value is corrupted in database."); } $saved_serialization = DB::query(Database::SELECT, ' SELECT data FROM serialized_object ORDER BY id DESC LIMIT 1 ')->execute()->get('data'); $saved_object = unserialize($saved_serialization); if (!$saved_object) { throw new Exception("Unable to unserialize object."); } if ($saved_object != $object) { throw new Exception("Saved object is not equal to object."); } $this->response->body('Everything is fine.'); } }
database.php:
<?php return array ( 'default' => array( 'type' => 'PDO', 'connection' => array( 'dsn' => 'mysql:host=127.0.0.1;dbname=test', 'username' => 'root', 'password' => '****', 'persistent' => FALSE, ), 'table_prefix' => '', 'charset' => 'utf8', 'caching' => FALSE, ), );
Scheme:
CREATE TABLE `serialized_object` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `data` longblob NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8