简体   繁体   English

从MySQL表反序列化数据并通过PHP输出?

[英]unserialize data from mysql table and output via php?

My wp_usermeta table has 4 columns, umeta_id | 我的wp_usermeta表有4列,umeta_id | user_id | user_id | meta_key | meta_key | meta_value Image of table: meta_value表格图片: 在此处输入图片说明

One of the columns has serialized data - wp_s2member_custom_fields. 列之一具有序列化数据-wp_s2member_custom_fields。 How can I unserialize from mysql or output with php to see all of my users data within the serialized column? 如何从mysql反序列化或使用php输出反序列化以查看序列化列中的所有用户数据?

Here is a breakdown of the serialized data: wp_s2member_custom_fields 这是序列化数据的细分:wp_s2member_custom_fields

a:12:{
s:7:"country";
s:2:"CA";
s:4:"city";
s:8:"Brampton";
s:5:"state";
s:7:"Ontario";
s:8:"zip_code";
s:6:"L6T4E7";
s:3:"age";
s:13:"25–34 years";
s:8:"blog_url";
s:22:"http://www.blog.com";
s:16:"blog_description";
s:106:"A blog about blogging";
s:15:"monthly_uniques";
s:4:"1000";
s:13:"facebook_page";
s:55:"http://www.facebook.com/myfacebookpage";
s:14:"facebook_likes";
s:3:"1428";
s:15:"twitter_account";
s:31:"http://twitter.com/mytwitterpage";
s:17:"twitter_followers";
s:3:"5849";}

Fetch the data from the database and use PHP's unserialize() . 从数据库中获取数据并使用PHP的unserialize() There's no way of doing this in MySQL (or any other DB) and it's the main reason that most developers prefer to just write comma-separated valies in their tables. 在MySQL(或任何其他数据库)中无法做到这一点,这是大多数开发人员更喜欢仅在表中编写逗号分隔的值的主要原因。

WordPress stores arrays/objects in the database by serializing them, so usually the retrieval function is what you want to look for since it will return the data unserialized. WordPress通过序列化将数组/对象存储在数据库中,因此检索功能通常是您要查找的内容,因为它将返回未序列化的数据。

In this case, it's user_metadata , so you need to use the function get_user_meta() , which you provide the $user_id and $key parameters to retrieve it. 在这种情况下,它是user_metadata ,因此您需要使用函数get_user_meta() ,该函数提供$ user_id和$ key参数来检索它。

In your example, it would be something like this: 在您的示例中,将是这样的:

<?php    
$array = get_user_meta(760, 'wp_s2member_custom_fields');
var_dump($array);



I hope that helps and makes sense... 我希望这会有所帮助并有意义...

NOTE: The WordPress Codex is a priceless resource! 注意: WordPress Codex是无价的资源!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM