0

I have the following serialized PHP array stored in a mySQL database:

a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}

Now, I managed to output that value with:

$my_data=mysql_result($result,$i,"my_data");
echo "$my_data";

but I can't manage to unserialize it. I tried this but it doesn't work:

$my_data=unserialize($my_data);

When I add that in between, all I get is a blank page. Any ideas?

5
  • Have you turned on display_errors? Or looked into your error logs. A blank screen often indicates some kind of error. Commented Apr 1, 2012 at 14:57
  • I just tried that and get this: Notice: unserialize() [function.unserialize]: Error at offset 12 of 62 bytes in test.php on line 25 Commented Apr 1, 2012 at 15:08
  • Have you checked the string returned from the db is identical to the string you put into the db? btw it's not usually considered good practice to store a serialised string in a db. Commented Apr 1, 2012 at 15:13
  • and var_dump shows bool(false) Commented Apr 1, 2012 at 15:14
  • Yeah, the string is the same. Unfortunately my software developer did it that way, not me :( I tested the serialized string on unserialize.net and it decoded it fine there, so I figure the problem must be on my end. Commented Apr 1, 2012 at 15:15

1 Answer 1

3

Maybe you should look at the process of inserting the the value into the database. Is it possible that after the values are serialized, that they were encoded in someway, such as to html entities or something?

I ran a test locally and I got the same error message. Here is output:

a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";} Notice: unserialize(): Error at offset 12 of 62 bytes in /srv/localhost/public_html/test.php on line 6 

Here is code

<?php

$value = htmlentities('a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}');
echo $value;

unserialize($value);
Sign up to request clarification or add additional context in comments.

1 Comment

that was the problem! The " was converted into HTML tags. I replaced them and now it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.