views:

159

answers:

1

What am I doing wrong here? I'm serializing a value, storing it in a database (table bb_meta), retrieving it... OK so far... but then I have to unserialize it twice. Shouldn't I be able to just unserialize once? This seems to work, but I'm wondering what point about serialization I'm missing here.

//check database to see if user has ever visited before.
$querystring = $bbdb->prepare( "SELECT `meta_value` FROM `$bbdb->meta` WHERE `object_type` = %s AND `object_id` = %s AND `meta_key` = %s LIMIT 1", $bbtype, $bb_this_thread, $bbuser );
$bb_last_visits = $bbdb->get_row($querystring, OBJECT);
//if $bb_last_visits is empty, add time() as the metavalue using bb_update_meta
    if (empty($bb_last_visits)) {
        $first_visit = time();
        echo 'serialized first visit: ' . $bb_this_visit_time_serialized = serialize(array($bb_this_thread => $first_visit));
        bb_update_meta( $bb_this_thread, $bbuser, $bb_this_visit_time_serialized, $bbtype ); //add to database, bb_meta table
        echo '$bb_last_visits was empty. Setting first visit time as ' . $bb_this_visit_time_serialized . '<br>';
        } else {
            //else, test by unserializing the data for use.
            echo 'last visit time already set: '; echo $bb_last_visits->meta_value; echo '<br>';
            //fatal error - echo 'unserialized: ' . $bb_last_visits_unserialized = unserialize($bb_last_visits[0]->meta_value); echo '<br>';
            echo 'unserialize: ' . $unserialized_visits = unserialize($bb_last_visits->meta_value); echo '<br>';
            echo 'hmm, need to unserialize again??: '; echo $unserialized_unserialized_visits = unserialize($unserialized_visits); echo '<br>';
            echo 'hey look, it\'s an array value I can finally use now. phew: ' . $unserialized_unserialized_visits[$bb_this_thread];
            }
A: 

bb_update_meta() is a bbPress function, which calls a Wordpress function called maybe_serialize() that "does the boring stuff for you" which apparently in this case means conditionally serializing an array.

An unserialized array can be directly passed to this function. By doing so, I avoided the need to un-un-serialize.

Not sure all the effort was worth the performance gain, but at least it was educational.

Thanks @webbiedave for setting off the spark that solved this!

55skidoo