I use php serialize() to serialize an array. Then I put it in the database (column type text). My array contains other language character like chinese or japanese characters.
It is able to serialize and store it in database correctly however, when I obtain the serialized array out of database and unserialize it so I can use the array, it won’t work, the unserialize array will just be blank.
here’s my code,
Save script:
$all = array (
"points" => '123',
"photo" => '写真',
"video" => 'video'
);
$sall = serialize($all);
mysql_query("UPDATE users SET lang = '$sall' WHERE uname='$uname'")
or die(mysql_error());
Retrieve script:
$result = mysql_query("SELECT * FROM users WHERE uname='$uname'")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row) {
$lang = $row['lang'];
$orilang = unserialize($lang);
// orilang contains the array
echo $orilang['photo'];
}
else {
}
$orilang['photo'] came out empty but $lang has the serialized data.
As
serialize()is not binary safe I’d suggest you to usejsonto encode you data:As a side note, if you started building something I’d suggest you move to MySQLi or PDO,
mysql_*functions are deprecated. Here is a nice tutorial to get you started on PDO.To get results: