Reputation: 185
I have the following serialized array stored in a MySQL longblob data field:
a:1:{s:10:"attributes";a:1:{s:13:"Ticket Holder";a:1:{i:0;s:8:"Joe Blow";}}}
In PHP, when I query the field, unserialize it, and print it out, the following empty array is printed:
Array
(
)
This is the table create statement:
CREATE TABLE `order` (
`state` varchar(255) CHARACTER SET ascii DEFAULT NULL,
`data` longblob,
`created` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Upvotes: 0
Views: 2001
Reputation: 185
Converting the column to UTF8 in the SELECT statement and then unserializing it.
CONVERT (data USING utf8)
I'm not sure why this is necessary. I'm guessing it has something to do with the utf8mb4_general_ci collation.
Thank you for all of your help guys!
Upvotes: 0
Reputation: 1398
base64_encode
The core problem comes from the binary encoding done by the database and the extra bytes it adds to the value stored. This "corrupts" the data that is being fetched from the database and causes unserialize
to fail. To help mitigate this problem, you can use base64_encode
and base64_decode
which will allow you to insert and extract the data painlessly.
// data is encoded before insert
$dataToInsert = base64_encode(serialize($myArray));
// decode the data before unserializing it
$dataToRead = unserialize(base64_decode($longblob));
Upvotes: 0
Reputation: 606
Your serialized data is invalid. You must never mainpulate serialized data manually.
Strings are serialized as:
s:<i>:"<s>";
where <i>
is an integer representing the string length of <s>
, and <s>
is the string value.
So in this case valid data is :
a:1:{s:10:"attributes";a:1:{s:13:"Ticket Holder";a:1:{i:0;s:8:"Joe Blow";}}}
Joe Blow
string length is 8 but in your serialized strings is defined 13.
See : Structure of a Serialized PHP string
Upvotes: 1