WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a widget which contains the following code:

<? global $wpdb;
$querystr = "SELECT name FROM wp_venues WHERE name ='".the_field('venue')."' ";
$info = $wpdb->get_var($querystr);
echo $info;

What I need is to be able to use the information from the table and format it, so


At the moment I am only able to retrieve 1 field, I think get_rows would help but how am I able to separate the fields so I can format.


share|improve this question
up vote 2 down vote accepted

"SELECT name FROM wp_venues WHERE name ='".the_field('venue')."' "; will only return the value of the_field('venue') which is already known.

"SELECT * FROM wp_venues WHERE name ='".the_field('venue')."' "; will select the whole row with all fields.

Try too use $wpdb->prepare for security reasons.

With $wpdb->get_row you get the whole row as either an object, a numerically indexed array or an associative array. See the codex on get_row.

For example you could do the following:

$row = $wpdb->get_row( $wpdb->prepare("SELECT * FROM wp_venues WHERE name = %s", the_field('venue')));
echo "Name: ".$row->name."\n";
echo "Capacity: ".$row->capacity."\n";
echo "Image: ".$row->image."\n";
echo "Description: ".$row->description."\n";


Try what is mentioned in the codex : http://codex.wordpress.org/Class_Reference/wpdb#Show_and_Hide_SQL_Errors and turn on WP_DEBUG in wp-config.php.

In addition : $wpdb->last_query can be looked up to show the last query used. Retrieve the query, that is actually executed and try this query manually and see which results there are.


share|improve this answer
Hi yes, thanks but what I need is to separate the results into variables that I can format, at the moment it selects the row as a whole. Any ideas. – Adam Beaumont Apr 2 '12 at 12:38
i edited the answer. do you see now, how you can access the values of the fields? – xaedes Apr 2 '12 at 12:56
Hi yes I see how you have done it but for some reason it is not working for me. None of the echo variables are working , only the name which is because of the_field('venue'). – Adam Beaumont Apr 2 '12 at 13:51
which query are you using? what is the structure of the table? how does $row look? use print_r($row) to examine it. – xaedes Apr 2 '12 at 13:56
I have tried both my query and yours above. The wp_venues table contains 5 columns, id, name, capacity, image, description. print_r($row) not showing anything. – Adam Beaumont Apr 2 '12 at 14:02

Your Answer


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.