在将对象存储到数据库之后,我在网站上有许多偏移错误,而不是检索它.如果我不存储它一切正常:
$serializedObject = serialize($this);
$unSerializedObject = unserialize($serializedObject);
Run Code Online (Sandbox Code Playgroud)
此外,我在保存数据并从数据库中检索数据时使用base64编码,但这没有用.我不做任何逃避.我的对象处理一些字符串.我发现的是这个字符串:
A woman is travelling around the world. She is 28 years old and she is from Great Britain.
She cannot use a car or a plane on her
Run Code Online (Sandbox Code Playgroud)
它工作正常.但是当我添加一个空格和单词[旅程]时,错误会弹出.这是带有这个单词的字符串:
A woman is travelling around the world. She is 28 years old and she is from Great Britain.
She cannot use a car or a plane on her journey
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么弹出错误?
这是serialize($this)没有单词的文本运行的输出journey
以下是serialize($this)使用单词对文本运行的输出journey
UPDATE
我保存对象的表有charset utf-8和没有定义charset 的列,因为它是BLOB类型.该mb_detect_encoding(serialize($this))收益UTF-8
没有逃脱$sql.这是在我正在使用的Kohana框架内执行查询的方式:
$result = mysql_query($sql, $this->_connection)
Run Code Online (Sandbox Code Playgroud)
原始答案:
MySQL中的TEXT字段最多可存储65535个字节,因此我的猜测是它被截断了.
请改用MEDIUMTEXT或LONGTEXT.
除此之外,如何将数据导入和导出数据库也存在潜在问题.PHP序列化字符串可以包含空字节(字节0),这似乎是没有正确传输的内容.
解决这个问题的一种方法是通过base64_encode()使用非常友好的字母数字/符号字母表来对字符串进行编码.如果您将BLOB类型增加到MEDIUMBLOB或,那将解决您的问题LONGBLOB.
但是,如果您正确地将查询发送到数据库,则可以安全地发送原始字符串.既然您正在使用Kohana,那么这个样本对我来说非常合适.
精简版:
$sql = 'INSERT INTO serialized_object (data) VALUES (:data)';
DB::query(Database::INSERT, $sql)->
param(':data', $serialization)->
execute();
Run Code Online (Sandbox Code Playgroud)
码:
<?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.');
}
}
Run Code Online (Sandbox Code Playgroud)
database.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,
),
);
Run Code Online (Sandbox Code Playgroud)
架构:
CREATE TABLE `serialized_object` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`data` longblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Run Code Online (Sandbox Code Playgroud)