I have the offset error like many on the website after storing object into database and than retrieving it. If I don't store it everything works fine:
$serializedObject = serialize($this);
$unSerializedObject = unserialize($serializedObject);
Besides, I use base64 encoding when saving data and retrieving it from database, but this doesn't help. I don't do any escaping though.
My object processes some string. What I've found out is that with this string:
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
It works fine. But when I add one more space and word [ journey], the error pops up. Here is the string with this one word:
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
My question is why does the error pop up?
Here is the output of the serialize($this)
run against the text without the word journey
Here is the output of the serialize($this)
run against the text with the word journey
UPDATE
The table I'm saving object into has charset utf-8
and the column with no charset defined since it's of BLOB type.
The mb_detect_encoding(serialize($this))
returns UTF-8
There is no escaping for the $sql
. This is how the query is executed inside Kohana framework that I'm using:
$result = mysql_query($sql, $this->_connection)
Best Solution
Original answer:
In addition to that, there are potential issues with how you get the data into and out of the database. PHP serialized strings can contain null bytes (the byte 0) and that appears to be what is not getting transfered properly.
One way to work around that is to encode the string via something like
base64_encode()
that uses a very friendly alphanumeric/symbol alphabet. That will solve your problems if you increase yourBLOB
type toMEDIUMBLOB
orLONGBLOB
.However, if you are properly sending your queries to the database you can safely send the original string. Since you are using Kohana, here is a sample that works perfectly fine for me.
Short version:
Code:
database.php:
Schema: