1

I'm using the Serialize function to store an array in my MYSQL database, and then I'm unSerialize Him in other page. The array structure look like this :

Array ( [0] => Array ( [names] => somename1 [rating] => 10 ) [1] => Array ( [names] => somename2 [rating] => 9 ) )

When I INSERT the array to the database I'm using this function to convert it to string :

$array_string=mysql_escape_string(serialize($arr));

And then, when I'm doing the unSerialize, I don't know how to restore the string(array in the database) to the exactly structure that it was before. (how to convers this string back to array)

I know I have to use this line :

$arr=explode("|",$list);

In some way, but I can't restore it to the exactly structure of the array it was before. The result of this line is a little bit different in the structure of the array :

Array ( [0] => Array( [0] => Array ( [names] => d [rating] => 7 ) [1] => Array ( [names] => b [rating] => 6 )  ) )

Thanks

2 Answers2

1

The opposite of serialize is unserialize.

$old_array = unserialize($serialized_array_string_from_db);

Storing values serialized into the database, disallows to query them individually. You have to fetch the serialized value, unserialize and then you can start working with them. This is not very efficient from a database design perspective. My suggestion is to create individual fields for "names" and "rating" in a extra table.


Store Array serialized to Database

$array = array('names' => 'somename1', 'rating' => 10);
$array_serialized_to_string = serialize($array);
doStoreToDb($array_serialized_to_string, somewhere);

Fetch Serialized Array from Database

$array_serialized_to_string = doFetchFromDb(somewhere);
$array = unserialize($array_serialized_to_string);

The difference in array structure might be the result from querying "the return set as array". Just remove the outer array:

$old_array = unserialize($serialized_array_string_from_db);
$array = $old_array[0];
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • This is not my problem, I have Highlighted (bold) the problem. – David Alexandrovich Oct 19 '14 at 11:39
  • you are right that I have to replace the $array = $old_array[0]; like this, but I think I didn't explain myself well. – David Alexandrovich Oct 19 '14 at 12:29
  • The problem is - When I'm trying to get "echo $arr[0]['names'];" for example, I get warning : "Illegal string offset 'names' in bla bla bla.." I think the problem is that $array is not recognized as array. because when I'm doing "echo $arr;" it's printing the arr like the print_r() function. I think it recognize it as a string. – David Alexandrovich Oct 19 '14 at 12:31
  • Ok, let's take a look at the structure of the array and it's types: var_dump($arr) – Jens A. Koch Oct 19 '14 at 15:41
  • array(1) { [0]=> string(505) "Array( [0] => Array ( [names] => d [rating] => 7 ) [1] => Array ( [names] => b [rating] => 6 ) [2] => Array ( [names] => a [rating] => 5 ) [3] => Array ( [names] => e [rating] => 5 ) [4] => Array ( [names] => c [rating] => 4 ) [5] => Array ( [names] => f [rating] => 3 ))" } By the way - Thank you man! – David Alexandrovich Oct 19 '14 at 15:50
  • Now that's a bit weird. How did you create that? – Jens A. Koch Oct 19 '14 at 15:59
  • I'm pushing it to the database this way : "$array_string=mysql_escape_string(serialize($arr));" and then insert the $array_string – David Alexandrovich Oct 19 '14 at 16:06
  • When I'm using the explode, this is the result : array(1) { [0]=> string(422) "Array( [0] => Array ( [names] => e [rating] => 5 ) [1] => Array ( [names] => d [rating] => 4 ) [2] => Array ( [names] => c [rating] => 3 ) [3] => Array ( [names] => b [rating] => 2 ) [4] => Array ( [names] => a [rating] => 1 ))" } – David Alexandrovich Oct 19 '14 at 16:11
  • This string gets a modification somewhere. Let's find out where. Please post a var_dump($array_string) directly after mysql_escape_string(serialize()). That should give us the serialized string before insertion into db. With adminer or phpmyadmin you should find exactly this string in your db. That's the insert part. Then: the fetching part. How do you query your db? – Jens A. Koch Oct 19 '14 at 16:15
  • When I do vae_dump($array_string) directly after, I get this : string(350) "s:339:\"Array( [0] => Array ( [names] => d [rating] => 4 ) [1] => Array ( [names] => c [rating] => 3 ) [2] => Array ( [names] => b [rating] => 2 ) [3] => Array ( [names] => a [rating] => 1 ))\";" – David Alexandrovich Oct 19 '14 at 16:25
  • $insert_into = "INSERT INTO table1 VALUES ('','$user_id','$list_name','$array_string') and then I run the query. – David Alexandrovich Oct 19 '14 at 16:28
  • I think the $array_string should start with "a:(sizeOfArray):keys" and not "s:339..". Please var_dump($arr) before serialize (to make sure that is an array before serialization and not a string). Where is $arr coming from? – Jens A. Koch Oct 19 '14 at 16:30
  • before the unserialize : string(339) "Array( [0] => Array ( [names] => d [rating] => 4 ) [1] => Array ( [names] => c [rating] => 3 ) [2] => Array ( [names] => b [rating] => 2 ) [3] => Array ( [names] => a [rating] => 1 ))" – David Alexandrovich Oct 19 '14 at 16:35
  • I'm sending the array from the same page by a POST form, by this input : – David Alexandrovich Oct 19 '14 at 16:36
  • 1
    Ok, i think we found it! :) Your array isn't an array anymore, but a string, because of print_r... print_r gives you a string. then you are are serializing the string again.. – Jens A. Koch Oct 19 '14 at 16:36
  • 1
    It's a bit complicated to turn the string from print_r() back to an array, but possible, see http://stackoverflow.com/questions/7231763/create-variable-from-print-r-output . In general it's better to use var_export($array, true), because the variable is easily converted back by a simple assign. - `` this piece of code needs a change. if you simply removed print_r here, the form content looks ugly, but the serializing to array should start to work... – Jens A. Koch Oct 19 '14 at 16:42
1

Try json_encode and json_decode

$array_to_store = array(....);
$str_array = json_encode($array_to_store);
//Store $str_array
//Retrieve it and to make it an array again
$array = json_decode($str_array, true);

******************* Edited *********************

I do not see what is wrong with serialize and unserialize:

$array = array(
        array(
                'names' => 'somename1',
                'rating' => 10
             ),
        array(
                'names' => 'somename2',
                'rating' => 9
             )
    );

//Array before
print_r('<pre>');
print_r($array);

//Serialised Array
$s = serialize($array);

print_r('<pre>');
print_r($s);

//Unserialised Array
print_r('<pre>');
print_r(unserialize($s));