PHP Notice: unserialize() ... : Error at offset 6 of 10 bytes in ... \includes\bootstrap.inc on line 428
Improperly Serialized Variables in Database
When loading a page the following error occurs when loading an improperly serialized array from the variables table. The same technique can be used for bad serialized data in other tables.
Error
PHP Notice: unserialize() [<a href='function.unserialize'>function.unserialize</a>]: Error at offset 6 of 10 bytes in \includes\bootstrap.inc on line 428
Depending on how caching is set on your site, you may only see this error sometimes. Because once the variables are cached they may not be loaded every time.
Some Causes of Serialization Issues
- Changing serialized content via sql replace command such as when migrating a site. You can't do this unless you follow up and put the
length of the new value in by hand. - Serializing resources
- Serializing objects and unserializing them without the class code loaded
- Serialized string is truncated because it is too long for the field it is stored in.
- Encoding/Decoding PHP/mysql issues
Debugging Technique
Around 480 of bootstrap.inc in drupal 6 change the code to as follows. (Set aside the old code to replace afterward) This will show you the variables that are throwing errrors. The @ symbol before unserialize suppresses the error so all the variables can be checked before code execution stops.
// if ($cached = cache_get('variables', 'cache')) {
// $variables = $cached->data;
// }
// else {
$result = db_query('SELECT * FROM {variable}');
while ($variable = db_fetch_object($result)) {
$variables[$variable->name] = @unserialize($variable->value);
if ($variables[$variable->name] === FALSE) {
print "<hr/>Unserialize Error for variable:". $variable->name . '='. $variables[$variable->name] . "<br/>". $variable->value;
}
}
die;
cache_set('variables', $variables);
// }
You may only get the error when variables are first loaded. After that they may be cached. So to reproduce the error, flush the cache via sql:
truncate table cache_block;
truncate table cache;
truncate table cache_content;
truncate table cache_filter;
truncate table cache_form;
truncate table cache_menu;
truncate table cache_views;Resolution
If you are getting errors with all or many of your variables, this technique will not work as the cause is likely not isolated bad record(s) in your variables table.
Once you find the bad variable(s), you will need to fix it in the database. (You may also override the variable by setting its value in the $conf array as described at the bottom of the settings.php file but you will not be able to change the variable value via the web interface until you remove the override in settings.php)
Below are what serialized arrays should look like. (look in the variables table):
name: node_options_bio
value: a:1:{i:0;s:6:"status";}
name: googleanalytics_track_6
value: b:0;The i represent integers, s strings, and the count is the length within the quotes.
You may be able to fix the value by hand editing it the database table. If you can't, you may just want to delete it, saving the old value somewhere, and let Drupal set it to its default. This may cause problems for some variables.
