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 the following:

$query = 'SELECT * FROM wp_pod_tbl_add_questions WHERE id LIKE '. $id;

                                        $row = $wpdb -> get_results($query);

How do I get the columns named 'id' and 'name' from $row?

share|improve this question
up vote 5 down vote accepted
foreach( $wpdb->get_results("SELECT * FROM your_table_name WHERE id LIKE' . $id . ';") as $key => $row) {
// each column in your row will be accessible like this
$my_column = $row->column_name;}

More info here

share|improve this answer
This is great, thanks! – redconservatory Jul 20 '11 at 14:46
not sure if this is the right way though. I think one should get the result to a variable and use foreach on that, to be safe. E.g. $results = $wpdb->get_results($sql); and then use foreach($results as $value). – Sugato Sengupta Sep 13 '13 at 11:15

Always Try the WordPress Codex: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

Essentially given the default syntax, the variable $row here is an object containing your results. You could alternately specify the TYPE of result (numeric array, associative array).

Assuming just one result, then $row->id and $row->name should give you the information.

If you get back more than one result, you'd want to loop over the entries in the object.

If you are expecting just one row back, then try using $wpdb->get_row http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Row

share|improve this answer

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.