0

The array in the database is stored as a serialized string, such as this:

a:1:{i:0;a:4:{s:8:"category";s:26:"Category Name";s:4:"date";s:0:"";s:8:"citation";s:617:"617 Char Length String (shortened on purpose)";s:4:"link";s:0:"";}}

It's structure should resemble the following when unseralized:

array {
    id => array { category => Value, date => Value, citation => Value, link => Value }
}

The php code I'm using is:

$prevPubs = unserialize($result[0]['citations']);

The $result[0]['citations'] is the serialized string. $prevPubs will return false. Which indicates an error if I'm not mistaken.

Any help would be greatly appreciated.

3 Answers 3

1

b:0 is boolean:false in serialized format. Unserialize would NOT return that exact string, it'd just return an actual boolean FALSE. This means that whatever you're passing into the unserialize call is not a valid serialized string. Most likely it's been corrupted somehow, causing the unserialize call to fail.

Sign up to request clarification or add additional context in comments.

1 Comment

You were right, twice. b:0 actually was getting inputted back into the table, and I just remembered where I saw b:0 incorrectly. I was getting a false, and it was because as Peter said I had a multidimensional array.
0

in order to handle serialized multidmensional arrays and mysql use this:

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

//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));
?>

i'm pretty sure serialized string in your database is corrupted

5 Comments

This was the problem. Thank you.
The database most certainly did not corrupt the string. The char-set encoding did. Make the field binary or double-check the char-set settings when you connect to your database.
I'm not exactly sure if this is what you mean, but I set the char-set to utf8-bin where it should of been. I have the field as a mediumtext field. Is there an advantage to having it as a blob? Lastly, I'm running into another issue with printing the array out. In the following code it has the unserialized, decoded array. Printing $result, shows this array as expected. foreach ($result as $array) { foreach ($array as $key => $value) { if ($value['link'] == '') { echo $value['citation']; } else { echo '<a href="'.$value['link'].'">'.$value['citation'].'</a>'; } } }
The issue is that it cycles through the link three times with the values P, O, and <, which aren't in the array at all.
Anyways mysql+php as long i remember gives me troubles with utf-8 charset. Check this doc php.net/manual/en/function.unserialize.php - people had same problem as you...
0

You have to unserialize the whole string $result = unserialize($serialized);

and then use the $result[0]['citation'] index of the result array.

1 Comment

The $result[0]['citations'] is actually the serialized string. I'll edit the original message to make it more clear.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.