• Advertisement
Sign in to follow this  

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

This topic is 3535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement