2

Error:

Notice:  unserialize() [function.unserialize]: Error at offset 0 of 126 bytes in C:\wamp\www\web_service\client.php on line 224

false

Code 1:

$data = array('table'=>'users', 'operation'=>'select', 'uid'=>'yoyo');

$data = serialize($data);
print_r(unserialize($data));

Code 2:

$data = array('table'=>'users', 'operation'=>'select', 'uid'=>'yoyo');

$data = base64_encode(serialize($data));
print_r(unserialize(base64_decode($data)));

Both of above gives same error. Any idea why?

Thanks

Looked at these;

One, Two, ....

Community
  • 1
  • 1
BentCoder
  • 12,257
  • 22
  • 93
  • 165

2 Answers2

2
$data = array('table'=>'users', 'operation'=>'select', 'uid'=>'yoyo');

$data = json_encode($data);

// Use either as array
print_r((array) json_decode($data));

//Or Json
echo $data;

Apparently JSON is a better solution so I use it instead. Thanks for contributions.

SinusQuell
  • 19
  • 7
BentCoder
  • 12,257
  • 22
  • 93
  • 165
  • Casting `(array)` after producing an object with `json_encode()` will work on this flat array, but would not convert all levels of a multi-level object to an array -- it would only affect the top level. If you truely want an array instead of an object, then just call `json_decode($data, true)`. This answer doesn't resolve whatever was wrong, it just avoids the problem. – mickmackusa Jul 22 '22 at 03:29
2

This kind of problem with unserialize may be related to database connection encoding.

If serialized string has been saved with different encoding, number of bytes in counter for unserialize function will not match...

smentek
  • 2,820
  • 1
  • 28
  • 32