使用mySQL / Wordpress从元数据列中获取数据

时间:2011-12-01 02:08:41

标签: mysql wordpress

我正在使用wordpress元数据来存储动态数据。 我碰到了一堵墙,需要一些帮助,我曾经有一段基本上是多维数组的元数据。 我遇到的问题是如何存储元数据我很难找到我需要的东西。

在底部我已经包含了wordpress如何存储数据(它是一列) 这是第一个条目在存储之前的样子

   shopper_wp_id:3

   shopper_status_time:2011-11-29 17:24:49

   shopper_status_comment:COMMENTS

   shopper_status:declined

   shopper_answers:["TEST1","TEST2","TEST3","TEST4","TEST5","TEST6"]

   shopper_preapproval_status:maybe

我需要做的是找到shopper_wp_id = 3且shopper_status =已拒绝的所有条目

如果这两个值彼此相邻但它们被一个不断变化的日期时间隔开。有什么想法吗?

a:4:{i:0;a:6:{s:13:"shopper_wp_id";s:1:"3";s:19:"shopper_status_time";s:19:"2011-11-29 17:24:49";s:22:"shopper_status_comment";s:8:"COMMENTS";s:14:"shopper_status";s:8:"declined";s:15:"shopper_answers";a:6:{i:0;s:5:"TEST1";i:1;s:5:"TEST2";i:2;s:5:"TEST3";i:3;s:5:"TEST4";i:4;s:5:"TEST5";i:5;s:5:"TEST6";}s:26:"shopper_preapproval_status";s:5:"maybe";}i:1;a:7:{s:13:"shopper_wp_id";s:4:"2063";s:19:"shopper_status_time";s:19:"2011-11-30 16:37:52";s:22:"shopper_status_comment";s:17:"sgdfsgfgdfdfgfhsh";s:14:"shopper_status";s:4:"paid";s:15:"shopper_answers";a:6:{i:0;s:10:"sdfsadfdfs";i:1;s:11:"dfgdsfgsdfg";i:2;s:10:"sdfgfdsfdg";i:3;s:9:"dgsdfgdfg";i:4;s:10:"sgdfsgfdgd";i:5;s:10:"sdfgfgfgds";}s:26:"shopper_preapproval_status";s:3:"yes";s:13:"blog_post_url";s:17:"http://google.com";}i:2;a:6:{s:13:"shopper_wp_id";s:4:"2916";s:19:"shopper_status_time";s:19:"2011-11-29 20:13:13";s:22:"shopper_status_comment";s:7:"dfbdfdf";s:14:"shopper_status";s:8:"declined";s:15:"shopper_answers";a:6:{i:0;s:15:"cvczxvzxcvzxcbz";i:1;s:11:"zcvfxzbxbxb";i:2;s:8:"zvzxcbzb";i:3;s:10:"zfdbfdbdfb";i:4;s:11:"zdfbdfbfbdf";i:5;s:6:"bfdfdh";}s:26:"shopper_preapproval_status";s:2:"no";}i:3;a:6:{s:13:"shopper_wp_id";s:4:"1614";s:19:"shopper_status_time";s:19:"2011-11-29 20:16:06";s:22:"shopper_status_comment";s:15:"sfdhfsdhsdfhdsh";s:14:"shopper_status";s:8:"declined";s:15:"shopper_answers";a:6:{i:0;s:8:"sdfsdfsd";i:1;s:15:"zvzfbdfbsdfbdbd";i:2;s:15:"dfgdsfhsfdsfhdf";i:3;s:17:"xfbfghfghnfsgnfgn";i:4;s:17:"dsfgshfdshsfdghsg";i:5;s:12:"sdffsdhsdfhh";}s:26:"shopper_preapproval_status";s:2:"no";}}

2 个答案:

答案 0 :(得分:1)

字符串是序列化的,因此您需要对其进行反序列化,然后您可以使用生成的结构(在本例中为关联数组)更轻松地获取所需的信息。例如

$data = 'a:4:{i:0;a:6:{s:13:"shopper_wp_id";s:1:"3";s:19:"shopper_status_time";s:19:"2011-11-29 17:24:49";s:22:"shopper_status_comment";s:8:"COMMENTS";s:14:"shopper_status";s:8:"declined";s:15:"shopper_answers";a:6:{i:0;s:5:"TEST1";i:1;s:5:"TEST2";i:2;s:5:"TEST3";i:3;s:5:"TEST4";i:4;s:5:"TEST5";i:5;s:5:"TEST6";}s:26:"shopper_preapproval_status";s:5:"maybe";}i:1;a:7:{s:13:"shopper_wp_id";s:4:"2063";s:19:"shopper_status_time";s:19:"2011-11-30 16:37:52";s:22:"shopper_status_comment";s:17:"sgdfsgfgdfdfgfhsh";s:14:"shopper_status";s:4:"paid";s:15:"shopper_answers";a:6:{i:0;s:10:"sdfsadfdfs";i:1;s:11:"dfgdsfgsdfg";i:2;s:10:"sdfgfdsfdg";i:3;s:9:"dgsdfgdfg";i:4;s:10:"sgdfsgfdgd";i:5;s:10:"sdfgfgfgds";}s:26:"shopper_preapproval_status";s:3:"yes";s:13:"blog_post_url";s:17:"http://google.com";}i:2;a:6:{s:13:"shopper_wp_id";s:4:"2916";s:19:"shopper_status_time";s:19:"2011-11-29 20:13:13";s:22:"shopper_status_comment";s:7:"dfbdfdf";s:14:"shopper_status";s:8:"declined";s:15:"shopper_answers";a:6:{i:0;s:15:"cvczxvzxcvzxcbz";i:1;s:11:"zcvfxzbxbxb";i:2;s:8:"zvzxcbzb";i:3;s:10:"zfdbfdbdfb";i:4;s:11:"zdfbdfbfbdf";i:5;s:6:"bfdfdh";}s:26:"shopper_preapproval_status";s:2:"no";}i:3;a:6:{s:13:"shopper_wp_id";s:4:"1614";s:19:"shopper_status_time";s:19:"2011-11-29 20:16:06";s:22:"shopper_status_comment";s:15:"sfdhfsdhsdfhdsh";s:14:"shopper_status";s:8:"declined";s:15:"shopper_answers";a:6:{i:0;s:8:"sdfsdfsd";i:1;s:15:"zvzfbdfbsdfbdbd";i:2;s:15:"dfgdsfhsfdsfhdf";i:3;s:17:"xfbfghfghnfsgnfgn";i:4;s:17:"dsfgshfdshsfdghsg";i:5;s:12:"sdffsdhsdfhh";}s:26:"shopper_preapproval_status";s:2:"no";}}';
$data = unserialize($data);
foreach ($data as $shopper){
    if(isset($shopper['shopper_wp_id']) && $shopper['shopper_wp_id'] == 3){
        //do something
    }
}

答案 1 :(得分:0)

谢谢大家,但我想出来了,不确定它是最好的,但似乎很快.. 我最终使用了mySQL的LOCATE和SUBSTRING与HAVING踢球者的组合。 下面是我的函数,然后是它创建的SQL。

function db_get_users_shops_by_shopper_status($shopper_wp_id, $limit = 0, $shopper_status) {
    global $wpdb;
    if (!is_array($shopper_status)) {
        $shopper_status = array($shopper_status);
    }
    $sql = "
    SELECT wp_posts . *,
        SUBSTRING(meta_value, 
          LOCATE('{s:13:\"shopper_wp_id\";s:" . strlen($shopper_wp_id) . ":\"" . $shopper_wp_id . "\";', meta_value), 
          LOCATE('s:15:\"shopper_answers\";', meta_value))
        AS shopper_entity
    FROM wp_posts, wp_postmeta 
    WHERE post_type='shoppertunity' 
        AND wp_posts.ID = wp_postmeta.post_id AND meta_key = 'shoppers'
        AND meta_value LIKE '%s:13:\"shopper_wp_id\";s:" . strlen($shopper_wp_id) . ":\"" . $shopper_wp_id . "\";%'
    HAVING shopper_entity LIKE ";
    for ($i = 0; $i < sizeof($shopper_status); ++$i) {
        if ($i > 0) {
            $sql .= " OR shopper_entity LIKE ";
        }
        $sql .= " '%s:14:\"shopper_status\";s:" . strlen($shopper_status[$i]) . ":\"" . $shopper_status[$i] . "\";%'";
    }

    if ($limit != 0) {
     $sql .= "LIMIT " . $limit;
     }
    //echo($sql ."<br/><br/>");
    $results = $wpdb -> get_results($sql);

    return $results;
}

SQL结果

SELECT 
`wp_posts`.*,
SUBSTRING(`meta_value`,
  LOCATE('{s:13:"shopper_wp_id";s:4:"2063";',
          `meta_value`),
  LOCATE('s:15:"shopper_answers";', `meta_value`)) AS `shopper_entity`

这   wp_posts,   wp_postmeta 哪里    post_type ='shoppertunity'和wp_postsID = wp_postmetapost_idmeta_key ='购物者'和meta_value喜欢'%S:13: “shopper_wp_id”; S:1: “3”;%' HAVING shopper_entity LIKE'%s:14:“shopper_status”; s:9:“completed”;%'或shopper_entity LIKE'%s:14:“shopper_status”; s:4:“已付款“%'