I feel like this should be possible.

I have a function within my class which pulls a single row from my custom table:

class Get_Data{

    public static function get_data_single_db()
        global $wpdb;
        $user_table_name = $wpdb->prefix . 'custom_table';
        $results = $wpdb->get_results(
            "SELECT *
                FROM $user_table_name
                WHERE unique_id = '$unique_id';"

        return $results;

The $results array contains 7 keys. At the moment, outside of the class I am using this to extract a single key value:


This works, but out of curiosity, could something like this work better:


and in the function:

public static function get_data_single_db($key)
return $results->key
  • Why should this not be possible? You can pass an argument. Where is your problem? – moped Jul 29 '19 at 11:01
  • I said "I feel like this should be possible". Using my code above it is not working as I expected so I asked here where the tweak was needed. – Shaun21uk Jul 29 '19 at 14:29
  • If your result is always one row with 7 fields, you should use $wpdb->get_row. Then you can return from your result as you wish like $result->$key, where $key may be the field you're looking for (e.g. 'first_name'). – moped Jul 29 '19 at 15:15
  • Ahhhh, let me try that out, sounds very promising! thanks man! – Shaun21uk Jul 29 '19 at 15:56
  • I get "Symbol '$key' is declared but not used" in the function. – Shaun21uk Jul 29 '19 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.