admin管理员组

文章数量:1194115

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 2 years ago.

Improve this question

I'm bumping my head against the wall here. I have searched here on SE and found several answers, but somehow they don't work for this scenario.

I have WordPress + MemberPress and I need to retrieve the current user's subscription status for subscription with ID = 48. MemberPress stores this value in their own table: mepr_subscriptions, field 'status'.

Now I need the string value of this status field.

I tried this:

$userID = get_current_user_id();
global $wpdb;
$table_name = $wpdb->prefix . "mepr_subscriptions";

$user = $wpdb->get_results(
    "SELECT * FROM $table_name
    WHERE user_id = $userID
    AND product_id = 48"
);

A var_dump of $user shows this:

array(1) { [0]=> object(stdClass)#12026 (34) { ["id"]=> string(3) "115" ["user_id"]=> string(2) "58" ["product_id"]=> string(2) "48" ["coupon_id"]=> string(1) "0" ["subscr_id"]=> string(20) "mp-sub-6328ec8074400" ["price"]=> string(5) "37.00" ["total"]=> string(5) "37.00" ["tax_amount"]=> string(4) "0.00" ["tax_rate"]=> string(5) "0.000" ["tax_desc"]=> string(0) "" ["tax_compound"]=> string(1) "0" ["tax_shipping"]=> string(1) "1" ["tax_class"]=> string(8) "standard" ["gateway"]=> string(13) "upsell_stripe" ["response"]=> NULL ["period"]=> string(1) "1" ["period_type"]=> string(6) "months" ["limit_cycles"]=> string(1) "0" ["limit_cycles_num"]=> string(1) "2" ["limit_cycles_action"]=> string(6) "expire" ["limit_cycles_expires_after"]=> string(1) "1" ["limit_cycles_expires_type"]=> string(4) "days" ["prorated_trial"]=> string(1) "0" ["trial"]=> string(1) "0" ["trial_days"]=> string(1) "0" ["trial_amount"]=> string(4) "0.00" ["trial_tax_amount"]=> string(4) "0.00" ["trial_total"]=> string(4) "0.00" ["status"]=> string(9) "suspended" ["created_at"]=> string(19) "2022-09-19 18:26:08" ["cc_last4"]=> string(0) "" ["cc_exp_month"]=> string(0) "" ["cc_exp_year"]=> string(0) "" ["token"]=> string(0) "" } }

I tried this:

$status = $user['status'];

Not working. $status remains empty.

I also tried this:

$json = json_encode($user);
$status = $json->status;

$json returns this:

[{"id":"115","user_id":"58","product_id":"48","coupon_id":"0","subscr_id":"mp-sub-6328ec8074400","price":"37.00","total":"37.00","tax_amount":"0.00","tax_rate":"0.000","tax_desc":"","tax_compound":"0","tax_shipping":"1","tax_class":"standard","gateway":"upsell_stripe","response":null,"period":"1","period_type":"months","limit_cycles":"0","limit_cycles_num":"2","limit_cycles_action":"expire","limit_cycles_expires_after":"1","limit_cycles_expires_type":"days","prorated_trial":"0","trial":"0","trial_days":"0","trial_amount":"0.00","trial_tax_amount":"0.00","trial_total":"0.00","status":"suspended","created_at":"2022-09-19 18:26:08","cc_last4":"","cc_exp_month":"","cc_exp_year":"","token":""}]  

The variable $status remains empty.

And I tried this:

$json = json_encode($user[0]);
$status = $json->status;

$json returns this:

{"id":"115","user_id":"58","product_id":"48","coupon_id":"0","subscr_id":"mp-sub-6328ec8074400","price":"37.00","total":"37.00","tax_amount":"0.00","tax_rate":"0.000","tax_desc":"","tax_compound":"0","tax_shipping":"1","tax_class":"standard","gateway":"upsell_stripe","response":null,"period":"1","period_type":"months","limit_cycles":"0","limit_cycles_num":"2","limit_cycles_action":"expire","limit_cycles_expires_after":"1","limit_cycles_expires_type":"days","prorated_trial":"0","trial":"0","trial_days":"0","trial_amount":"0.00","trial_tax_amount":"0.00","trial_total":"0.00","status":"suspended","created_at":"2022-09-19 18:26:08","cc_last4":"","cc_exp_month":"","cc_exp_year":"","token":""}

The variable $status remains empty.

What am I missing or doing wrong?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 2 years ago.

Improve this question

I'm bumping my head against the wall here. I have searched here on SE and found several answers, but somehow they don't work for this scenario.

I have WordPress + MemberPress and I need to retrieve the current user's subscription status for subscription with ID = 48. MemberPress stores this value in their own table: mepr_subscriptions, field 'status'.

Now I need the string value of this status field.

I tried this:

$userID = get_current_user_id();
global $wpdb;
$table_name = $wpdb->prefix . "mepr_subscriptions";

$user = $wpdb->get_results(
    "SELECT * FROM $table_name
    WHERE user_id = $userID
    AND product_id = 48"
);

A var_dump of $user shows this:

array(1) { [0]=> object(stdClass)#12026 (34) { ["id"]=> string(3) "115" ["user_id"]=> string(2) "58" ["product_id"]=> string(2) "48" ["coupon_id"]=> string(1) "0" ["subscr_id"]=> string(20) "mp-sub-6328ec8074400" ["price"]=> string(5) "37.00" ["total"]=> string(5) "37.00" ["tax_amount"]=> string(4) "0.00" ["tax_rate"]=> string(5) "0.000" ["tax_desc"]=> string(0) "" ["tax_compound"]=> string(1) "0" ["tax_shipping"]=> string(1) "1" ["tax_class"]=> string(8) "standard" ["gateway"]=> string(13) "upsell_stripe" ["response"]=> NULL ["period"]=> string(1) "1" ["period_type"]=> string(6) "months" ["limit_cycles"]=> string(1) "0" ["limit_cycles_num"]=> string(1) "2" ["limit_cycles_action"]=> string(6) "expire" ["limit_cycles_expires_after"]=> string(1) "1" ["limit_cycles_expires_type"]=> string(4) "days" ["prorated_trial"]=> string(1) "0" ["trial"]=> string(1) "0" ["trial_days"]=> string(1) "0" ["trial_amount"]=> string(4) "0.00" ["trial_tax_amount"]=> string(4) "0.00" ["trial_total"]=> string(4) "0.00" ["status"]=> string(9) "suspended" ["created_at"]=> string(19) "2022-09-19 18:26:08" ["cc_last4"]=> string(0) "" ["cc_exp_month"]=> string(0) "" ["cc_exp_year"]=> string(0) "" ["token"]=> string(0) "" } }

I tried this:

$status = $user['status'];

Not working. $status remains empty.

I also tried this:

$json = json_encode($user);
$status = $json->status;

$json returns this:

[{"id":"115","user_id":"58","product_id":"48","coupon_id":"0","subscr_id":"mp-sub-6328ec8074400","price":"37.00","total":"37.00","tax_amount":"0.00","tax_rate":"0.000","tax_desc":"","tax_compound":"0","tax_shipping":"1","tax_class":"standard","gateway":"upsell_stripe","response":null,"period":"1","period_type":"months","limit_cycles":"0","limit_cycles_num":"2","limit_cycles_action":"expire","limit_cycles_expires_after":"1","limit_cycles_expires_type":"days","prorated_trial":"0","trial":"0","trial_days":"0","trial_amount":"0.00","trial_tax_amount":"0.00","trial_total":"0.00","status":"suspended","created_at":"2022-09-19 18:26:08","cc_last4":"","cc_exp_month":"","cc_exp_year":"","token":""}]  

The variable $status remains empty.

And I tried this:

$json = json_encode($user[0]);
$status = $json->status;

$json returns this:

{"id":"115","user_id":"58","product_id":"48","coupon_id":"0","subscr_id":"mp-sub-6328ec8074400","price":"37.00","total":"37.00","tax_amount":"0.00","tax_rate":"0.000","tax_desc":"","tax_compound":"0","tax_shipping":"1","tax_class":"standard","gateway":"upsell_stripe","response":null,"period":"1","period_type":"months","limit_cycles":"0","limit_cycles_num":"2","limit_cycles_action":"expire","limit_cycles_expires_after":"1","limit_cycles_expires_type":"days","prorated_trial":"0","trial":"0","trial_days":"0","trial_amount":"0.00","trial_tax_amount":"0.00","trial_total":"0.00","status":"suspended","created_at":"2022-09-19 18:26:08","cc_last4":"","cc_exp_month":"","cc_exp_year":"","token":""}

The variable $status remains empty.

What am I missing or doing wrong?

Share Improve this question edited Sep 22, 2022 at 15:16 Maurice asked Sep 20, 2022 at 13:34 MauriceMaurice 454 bronze badges 1
  • Memberpress and other 3rd party plugin/theme dev support is off topic here and not in this stacks scope. You should ask via their official support routes or in their groups and communities. – Tom J Nowell Commented Sep 20, 2022 at 14:49
Add a comment  | 

1 Answer 1

Reset to default 1

In the first two cases, there's an array wrapping the object that you have to attend to first. e.g. this should work:

$status = $user[0]->status;

As a sidenote:

In the last case, json_encode() doesn't, as the name might suggest, turn data into an object with properties that you can access — it turns it into a JSON string. Not very useful here.

You can use json_decode() again to turn the string into an object — but of course why bother, since $user[0] is the object you want in the first place.

本文标签: phpHow to get a database field value from a WordPress table