1

I'm trying to figure out why my query isn't running, and if it is; why isn't it returning any info. I'm running this query on wordpress multisite based on a plugging that goes through all network sites and retrieves all posts in the main site, that part is working fine. I created a bit of code to merge two variables into one variable to create a table name, if I echo that variable it displays the table name correctly, but when i insert the same variable in the query noting happens even dough the table already exists in the DB. the code is below:

 // Switch to the blog
 switch_to_blog($all_blogkeys[$field->guid]);
 global $wpdb;
 $prefix = $wpdb->prefix;  //get current site table prefix
 $table = 'rex_listings';  // name to attach to prefix
 $tablename = $prefix.''.$table;  // combine prefix and name
 //start the query and echo results
 $result = $wpdb->get_results('SELECT price, bedrooms, baths, floor_space, formatted_address FROM $tablename');
 foreach($result as $row) {
 echo 'Price: '.$row->price.', Bedrooms: '.$row->bedrooms.', Baths: '.$row->baths.', Floor Space: '.$row->floor_space.', Address: '.$row->formatted_address.',<br/>';}

Thanks in advance.

2
  • please check on this query in die('SELECT price, bedrooms, baths, floor_space, formatted_address FROM $tablename'); what it should be result Commented Feb 9, 2014 at 5:30
  • that kills the query and breaks the plugin, thanks for trying to help. Commented Feb 9, 2014 at 6:13

1 Answer 1

2

You need to use double quotes around the query and curly brackets around the variable, like so:

 $result = $wpdb->get_results("SELECT price, bedrooms, baths, floor_space, formatted_address FROM {$tablename}");
Sign up to request clarification or add additional context in comments.

1 Comment

That works great thanks for the fast reply all I need to do now is a join between post id and listing id. thanks a lot.

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.