English 中文(简体)
如何从mysql中的meta_value中提取某些数据
原标题:How to pull certain data from a meta_value in mysql

我在wordpress中有一个表,它是wp_usermeta,带有meta_key文件和一个名为wp_s2member_custom_fields的值,其中包含一组串联值,如下所示。

如何提取该值内monthly_uniques旁边的数据?

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:"128";
s:15:"twitter_account";
s:31:"http://twitter.com/mytwitterpage";
s:17:"twitter_followers";
s:3:"392";}

事实上,所有这些值都在一行上,为了清楚起见,请将它们放在单独的行上

我需要以不同的方式提取这些数据来显示报告。我该如何添加以下语句来拉取montly_uniques、city、zip_code等?

问题回答

当Wordpress将数组输入其数据库时,它会执行一种名为Serialization的操作,将数组转换为您提供的字符串的格式。要反转这个过程并返回相同的数组,只需通过PHP内置的unserialize()函数,该函数将以上面提到的方式返回一个关联数组。

所以这样的事情:

$string =  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:"128";s:15:"twitter_account";s:31:"http://twitter.com/mytwitterpage";s:17:"twitter_followers";s:3:"392";} ;

$array = unserialize($string);

$array[ monthly_uniques ]; //This will give you the monthly_uniques field.




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签