如何查询stdclass对象以从变量中获取值并与其进行求和?

时间:2011-08-14 17:40:44

标签: php mysql wordpress wpdb

我有一个名为wp_mgm_transactions的wordpress表,它有两个文本字段和几行记录:MODULE和DATA。

在每个字段DATA中我都有这个值/结构(这是一个stdclass对象吗?):     { “ID”: “3”, “membership_type”: “构件”, “持续时间”:1, “duration_type”: “L”, “成本”: “10.00”, “num_cycles”:1, “角色”:” subscriber“,”default“:”1“,”description“:”acesso a todos os conte \ uffados do site。“,”hide_old_content“:”0“,”active“:”1“,”sort“:”3 ”, “trial_on”: “0”, “trial_duration”: “0”, “trial_duration_type”: “d”, “trial_cost”: “0.00”}

enter image description here

我需要进行查询以获取行中所有数据的'cost'总和WHERE module ='paypal'

我知道怎么做,如果DATA字段是数字字段,它会这样:

$SumTotalData = $wpdb->get_var("SELECT sum(data) FROM wp_mgm_transactions WHERE module = 'paypal'");

但是,对于以上所有数据类型字段,我都输了。有帮助吗? 谢谢, 纳尔逊

1 个答案:

答案 0 :(得分:0)

这是否适合您的需要?

$ find =你在寻找什么 $ resultlength =假设结果的长度,在这种情况下 10.00 是5个字符长。 $ text =在

中搜索什么
$find = 'cost'; 
$resultlength = 5;
$text = '{"id":"3","membership_type":"member","duration":1,"duration_type":"l","cost":"10.00","num_cycles":1,"role":"subscriber","default":"1","description":"Acesso a todos os conte\u00fados do site.","hide_old_content":"0","active":"1","sort":"3","trial_on":"0","trial_duration":"0","trial_duration_type":"d","trial_cost":"0.00"}';

$result = substr(strstr($text, $find), strlen($find)+3, $resultlength);    
echo $result;