0
$array = array('1','2','3','4','5','6','10','100');
$array2 = serialize($array);
if($stmt->prepare("UPDATE `users` SET `dungeon_list` = ? WHERE name = ?")) { $stmt->bind_param('bs',$array2,$user['name']); $stmt->execute(); }

This is what I've learned to use on how to store arrays into mysql.

echo unserialize($user['dungeon_list']);

And this is how I output it. It says Notice: unserialize() [function.unserialize]: Error at offset 0 of 140 bytes in C:\wamp\www\index.php on line 12

Dungeon_list is stored as a blob because that what I've learned to use to store it. Thank you!

3
  • 1
    Storing arrays as strings inside database tables is, to me, like fingernails down a blackboard ... this just screams for another table like dungeon_to_user :\ Commented Apr 19, 2012 at 13:33
  • I'd love to do that. Each dungeon has 100 spots. I want to track if they have been to each spot before they can advance to the next one. I'd like to add bigger and bigger dungeons thought. Would making 100 column table be bad? Commented Apr 19, 2012 at 13:41
  • 1
    You can add more table: dungeon_spots to store the spots in a dungeon, and dungeon_user_spots to track the spots a user has visited in a given dungeon. Commented Apr 19, 2012 at 13:55

2 Answers 2

2

Try to encode and decode your array before serialize and unserialize because if it has any ", ', :, or ; in any of the array values the serialization gets corrupted.

//to safely serialize
$safe_string_to_store = base64_encode(serialize($multidimensional_array));

//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));
Sign up to request clarification or add additional context in comments.

Comments

0

I don't have any idea what is the content of your $user['dungeon_list'], it's better to provide the contents.

Anyway, it's probably cause by PHP magic_quotes_gpc directive. It might makes sense to check if it is set to ON, if yes might as well set it to OFF. Provide some depth details like where/how did you get the contents of your $user['dungeon_list'] and showing a var_dump or print_r of it.

Comments

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.