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 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

Name:
Capacity:
Image:
Description:

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.

Thanks

share|improve this question
add comment

1 Answer

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";

Debugging

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.

Literature

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
show 14 more comments

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.