Object of class wpdb could not be converted to string

I’m trying to get an organisation’s ID by giving its name.
I have the following code:

$orgid= $wpdb->get_var("SELECT organisationid FROM $wpdb->$table_name1 WHERE organisationname=' " . $organisation . "'");

This line returns this error:
Catchable fatal error:

Read More
Object of class wpdb could not be converted to string

I wish to echo this ID onto the page.

I have read the codex but do not understand. How can I fix this?

Related posts

2 comments

  1. If you’re going to use variable variables inside the string, remember to wrap it with curly braces:

    "SELECT organisaitonid FROM {$wpdb->$table_name1} WHERE organisaitonname = '" . $organisation . "'"
                           //    ^  important       ^
    

    Note: Just make sure that the value of $table_name1 does correspond to $wpdb‘s properties of course so that it matches.

  2. Try also

    $wpdb->table_name1
    

    instead of:

    $wpdb->$table_name1
    

    without dollar “$”

Comments are closed.