I'm new to PHP and using serialized data,
I have a Database which has a 2 tables, user & character. characterId has a relation with userId.
An user has 2 characters, so I wanted to get the data with the following code:
public static function getCharacter() {
$mysqli = Controller_Core_Config::getDB();
if ($mysqli != null) {
$user = unserialize($_SESSION['user']);
$sql = "SELECT * FROM `character` WHERE `userId`='" . $user->getId() . "'";
$result = $mysqli -> query($sql);
if ($result !== FALSE && $result -> num_rows > 0) {
$row = $result -> fetch_assoc();
$_SESSION["character"] = serialize(new Model_Game_User(
$row['characterId'],
$row['characterName'],
$row['userId'],
$row['level']
));
} else {
echo "You have no characters.";
}
}
}
And If I wanted to show the data I use the following code:
//some code
$character = unserialize($_SESSION["character"]);
$output .= $character->getCharacterName() . "<br>";
//some code
My problem is, that when I var_dump the ($_SESSION["character"]) I get only 1 character:
string(202) "O:15:"Model_Game_User":4:{s:28:"Model_Game_UsercharacterId";s:1:"3";s:30:"Model_Game_UsercharacterName";s:9:"adminious";s:23:"Model_Game_UseruserId";s:1:"2";s:22:"Model_Game_Userlevel";s:1:"3";}"
And my question is, is it possible to have multiple 'characters' in serialized data?