我试图从数据库查询中反序列化longtext(utf8_unicode_ci),但是当我这样做时,我收到以下错误。
unserialize(): Error at offset 6 of 737 bytes
我猜测我的字符串无效,因此我将其输出到浏览器。我注意到字符串的长度比实际应该的长一点。当我在浏览器中复制并粘贴输出并将其硬编码到php中时,sting长度会变为较小的,并且反序列化会起作用。
数据库字符串......
ASCII - string(737) "a:10:{s:2:"id";i:2234950;s:13:"full_describe";s:11:"Brace start";s:6:"person";s:17:"Dr Aalok Y Shukla";s:8:"datetime";s:25:"2014-01-06T09:00:00+00:00";s:8:"duration";i:30;s:11:"on_waitlist";b:0;s:10:"company_id";s:5:"35927";s:8:"attended";b:1;s:6:"_links";a:2:{s:4:"self";a:1:{s:4:"href";s:60:"http://uk.bookingbug.com/api/v1/admin/35927/bookings/2234950";}s:6:"client";a:1:{s:4:"href";s:57:"http://uk.bookingbug.com/api/v1/admin/35927/client/809828";}}s:11:"appointment";a:1:{s:11:"description";s:11:"Brace start";}}"
将字符串复制并粘贴到php ...
ASCII - string(517) "a:10:{s:2:"id";i:2234950;s:13:"full_describe";s:11:"Brace start";s:6:"person";s:17:"Dr Aalok Y Shukla";s:8:"datetime";s:25:"2014-01-06T09:00:00+00:00";s:8:"duration";i:30;s:11:"on_waitlist";b:0;s:10:"company_id";s:5:"35927";s:8:"attended";b:1;s:6:"_links";a:2:{s:4:"self";a:1:{s:4:"href";s:60:"http://uk.bookingbug.com/api/v1/admin/35927/bookings/2234950";}s:6:"client";a:1:{s:4:"href";s:57:"http://uk.bookingbug.com/api/v1/admin/35927/client/809828";}}s:11:"appointment";a:1:{s:11:"description";s:11:"Brace start";}}"
如您所见,字符串相同,但数据库调用中的字符串显示偏移量错误。
我是否需要对从数据库返回的字符串进行某种解码或格式化?
由于
答案 0 :(得分:2)
问题是我正在序列化导致问题的多维数组。
在使用序列化之前,只修复base64_encode()字符串,反之亦然。
//to safely serialize
$safe_string_to_store = base64_encode(serialize($multidimensional_array));
//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));
文章发现于http://davidwalsh.name/php-serialize-unserialize-issues
希望这有助于其他人