3

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
  • 11,419
  • 20
  • 88
  • 154
  • This works fine on my system. What version of PHP are you using, and is this *exactly* the code you are running? Try it on a command line, rather than a web server, if you can. – Adam Wright Feb 18 '13 at 13:30

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 better solution so I use it instead. Thanks for contributions.

Community
  • 1
  • 1
BentCoder
  • 11,419
  • 20
  • 88
  • 154
1

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,562
  • 1
  • 25
  • 32