1

I have a need to build a dynamic query that requires a variable to structured like so;

$my_var = $var_1.'_'.$var_2.'_band_'.$var_3;

$my_var is then used within a loop.

While I can echo the $my_var data successfully, this does not work in my loop. Is this even a possibility?

Here is the actual loop. $my_var would be used as a lookup for a column title. The vars used in $my_var would be strings.

global $wpdb;

    $my_var = $var_1.'_'.$var_2.'_band_'.$var_3;

    $results = $wpdb->get_results ( "SELECT * FROM quote WHERE Postcode_area LIKE '%$find%'" );

    foreach ( $results as $result ) {
        $savings = $result->$my_var * 52;
    }

SOLVED

Thank you everyone for your input, I'm sorry to say I was being a complete idiot! The column I was using to lookup data as a test did not required one additional VAR. Truly sorry to waste anyones time!

The loop is now working without issue.

5
  • 1
    You need to be more specific about what you are trying to do. Commented Dec 9, 2015 at 5:05
  • Which variables do you have? And what you have tried so far? Commented Dec 9, 2015 at 5:08
  • what have you tried so far? what is your current output? what is your expected output? Commented Dec 9, 2015 at 5:09
  • I have updated the question. The expected output would be a INT result from a column lookup which is then used as a sum. Commented Dec 9, 2015 at 5:15
  • Try using braces: $result->{$my_var} Commented Dec 9, 2015 at 5:19

1 Answer 1

2

As I mentioned in my comment, try using braces:

$result->{$my_var}

This practice is demonstrated in the manual a quarter of the way down under the "Complex (curly) syntax": http://php.net/manual/en/language.types.string.php

Sign up to request clarification or add additional context in comments.

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.