1

I have a DB table named 'affiliate_wp_coupons' where has 3 cols and they are;

  1. coupon_id
  2. affiliate_id
  3. coupon_code

I have coupon_code which is coming from a variable, not static. Now I wanted to query the associated affilaite_id value based on the coupon_code.

I am trying this code:

$results = $wpdb->get_results( "SELECT * FROM affiliate_wp_coupons WHERE `coupon_code`= 'BS0PDDTTGU'");

This code returns an Object with the matched col.

In the above code coupon_code value is static, how can I put a variable here?

2 Answers 2

1
$coupon_code = 'BS0PDDTTGU';
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM affiliate_wp_coupons WHERE `coupon_code`= %s", $coupon_code));
Sign up to request clarification or add additional context in comments.

Comments

0

There're multiple ways to concatenate/inserting variable into a string in PHP, ex:

$coupon_code = 'Windy';
$str = "Hello {$coupon_code}!"; // Hello Windy!
$str2 = "Hello $coupon_code!"; // Hello Windy!
$str3 = 'Hello '.$coupon_code.'!'; // Hello Windy!

personally I prefer $str2.

Then in your case you can replace your code something like:

$results = $wpdb->get_results( "SELECT * FROM affiliate_wp_coupons WHERE `coupon_code`= '{$coupon_code}'");

Note: don't forget to define $coupon_code before run query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.