如何从数据库中获取特定属性

时间:2020-01-24 作者:user3748874

a:3:{
  i:0;a:2:{s:21:"wwsd_percent_discount";s:2:"10";s:25:"wwsd_minimum_discount_qrt";s:2:"20";}
  i:1;a:2:{s:21:"wwsd_percent_discount";s:2:"20";s:25:"wwsd_minimum_discount_qrt";s:2:"50";}
  i:2;a:2:{s:21:"wwsd_percent_discount";s:2:"30";s:25:"wwsd_minimum_discount_qrt";s:3:"100";}
}
我想从wwsd_percent_discount.

我编写了以下代码:

$getId = $product->get_id();
global $wpdb;    
$result = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id=\'$getId\' AND meta_key =\'wwsd_wholesale_discount_data\'");

if ( !empty( $result ) ) {

    foreach ( $result as $r ) {
        echo "<br>".$r->meta_value;
    }
}

1 个回复
SO网友:Talk Nerdy To Me

如果您在问题开头放置的数据是数据库查询的结果(我不确定您的问题是如何措辞的),那么您将希望获取该查询结果并使用PHP函数unserialize() 若要将该数据转换为数组,则可以从该数组访问数据。

序列化数据将在如下数组中显示:

Array
(
    [0] => Array
        (
            [wwsd_percent_discount] => 10
            [wwsd_minimum_discount_qrt] => 20
        )

    [1] => Array
        (
            [wwsd_percent_discount] => 20
            [wwsd_minimum_discount_qrt] => 50
        )

    [2] => Array
        (
            [wwsd_percent_discount] => 30
            [wwsd_minimum_discount_qrt] => 100
        )
)