原答案:
MySQL 中的 TEXT 字段最多存储 65535 个字节,所以我猜它在那里被截断。
请改用 MEDIUMTEXT 或 LONGTEXT。
除此之外,如何将数据输入和输出数据库还存在潜在问题。PHP 序列化字符串可以包含空字节(字节 0),这似乎是没有正确传输的内容。
base64_encode()
解决此问题的一种方法是通过使用非常友好的字母数字/符号字母表的方式对字符串进行编码。如果您将BLOB
类型增加到MEDIUMBLOB
or ,那将解决您的问题LONGBLOB
。
但是,如果您正确地将查询发送到数据库,则可以安全地发送原始字符串。由于您使用的是 Kohana,因此这里有一个对我来说非常适合的示例。
精简版:
$sql = 'INSERT INTO serialized_object (data) VALUES (:data)';
DB::query(Database::INSERT, $sql)->
param(':data', $serialization)->
execute();
代码:
<?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.');
}
}
数据库.php:
<?php
return array
(
'default' => array(
'type' => 'PDO',
'connection' => array(
/**
* The following options are available for PDO:
*
* string dsn Data Source Name
* string username database username
* string password database password
* boolean persistent use persistent connections?
*/
'dsn' => 'mysql:host=127.0.0.1;dbname=test',
'username' => 'root',
'password' => '****',
'persistent' => FALSE,
),
/**
* The following extra options are available for PDO:
*
* string identifier set the escaping identifier
*/
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
);
架构:
CREATE TABLE `serialized_object` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`data` longblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8