Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have multiple sites setup within my site. I need to access all the various options tables which are named 'wp_#_options'. How can I select these tables that have a number prefix?

Using

$wpdb->get_results($wpdb->prepare("SELECT * FROM ".$wpdb->7_options." WHERE ....."));

gives me a parse error.

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

When in multisite, the wpdb prefix is always the prefix required to access the tables for the current site. This includes the site ID

so this should work:

$wpdb->get_results($wpdb->prepare("SELECT * FROM ".$wpdb->prefix."options WHERE ....."));

There should be no difference at all when using multisite, just make your queries as if it were single site.

share|improve this answer
add comment

I think there are a couple of ways to what you are doing based on your setup. If you just want to add the table prefix to the code this could work.

"SELECT * FROM %s", "{$wpdb->prefix}options" WHERE...

The $wpdb->prefix part uses the default WP table prefix from the main config file. You could create your own variable using information from another source if available.

Basic example...

$table = "wp_7";
"SELECT * FROM %s", "{$table}_options" WHERE...

I've not tried this though so can't be entirely sure it's correct. Hope it helps!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.