Sign in to follow this  
schloob2

[web] Problem with PHP serialize(), unserialize()

Recommended Posts

Hi. I am currently having a problem with serialize () and unserialize (). My game will have a technology tree and each player will be able to research what they want. I need to store researched technologies in an array for each person. for example: $technology[1] = 'Exploration Bonus', $technology[2] = 'Combat Bonus'. I have been able to store an array in MySQL but I haven't been able to retrieve it. When I echo the retrieved value it says Array or gives an error. I included my working method for storing the array and my faulty method for retrieving it. How do I correctly implement unserialize ()? Thanks //SAMPLE OF STORING SERIALIZED ARRAY SEEMS TO BE WORKING/// $technology[1] = "Exploration I"; $technology[2] = "Exploration II"; $techarray = array ( $technology ); $technology = serialize($techarray); $query = "UPDATE kingdom SET technology = '$technology' WHERE member_id = '$sessionid'"; $result = mysql_query($query); ///THIS DOESN'T WORK//// $query = "SELECT technology FROM kingdom WHERE member_id = '$sessionid'"; $result = mysql_query($query); $counter = 1; while($row=mysql_fetch_array($result)) { $technology = unserialize($row['technology']); } ///HOW DO I MAKE $technology SPLIT BACK INTO AN ARRAY (e.g. $technology[1], $technology[2], etc. etc.)////

Share this post


Link to post
Share on other sites
What is the point of this line:
$techarray = array ( $technology );

$technology is already an array. You seem to be putting the first array inside another array.

Try getting rid of the "$techarray" line (above) and just using:
$technology = serialize($technology);

[EDIT]
Also, if you want to print out an array for debugging purposes, use "print_r($technology)" or "var_dump($technology)" instead of "echo $technology". I don't think that echo can print out arrays properly, but the other two can.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this