为什么我必须反序列化这个序列化值两次? (wordpress/bbpress Maybe_serialize)

发布于 2024-08-27 21:00:02 字数 1723 浏览 3 评论 0 原文

我在这里做错了什么?我正在序列化一个值,将其存储在数据库(表 bb_meta)中,检索它...到目前为止还好...但随后我必须将其反序列化两次。我不应该只能反序列化一次吗?这似乎有效,但我想知道我在这里缺少序列化的哪一点。

//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];
            }

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];
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦亦如幻 2024-09-03 21:00:02

bb_update_meta() 是一个 bbPress 函数,它调用名为 Maybe_serialize() 的 Wordpress 函数,该函数“为你做那些无聊的事情",显然在这种情况下意味着有条件地序列化数组。

未序列化的数组可以直接传递给此函数。通过这样做,我避免了取消序列化的需要。

不确定所有的努力是否值得获得性能提升,但至少它是有教育意义的。

感谢@webbiedave 引发了解决这个问题的火花!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文