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

Why is it? I tried the same query in the console and it returned multiple rows. Here's the query:

$this->wpdb->get_row("SELECT * FROM ".$this->wpdb->users." WHERE status = 'active'", ARRAY_A);

It keeps returning the same single row when there are several active users. Am I missing something?

share|improve this question
up vote 17 down vote accepted

Indeed, use get_row() only when you expect to get one result, else you can use get_results()

share|improve this answer

There are three ways to pull data from the database.

1.$wpdb->get_var:use this to get a single value from the database table. Like if you want to count the total number of comments. You can do it in following way:

$comment_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments;")); 
echo '<p>Total comments: ' . $comment_count . '</p>';

2.$wpdb->get_row : To retrieve an entire table row you can use this.


$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ) );
echo $thepost->post_title; 


$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ), ARRAY_A );
print_r ($thepost); 

By using the ARRAY_A parameter in get_row your post data is returned as an associative array. Alternatively, you could use the ARRAY_N parameter to return your post data in a numerically indexed array.

3.$wpdb->get_results:Standard SELECT queries should use the get_results function for retrieving multiple rows of data from the database.

$allposts = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish'") );
foreach ($allposts as $singlepost) { 
         echo '<p>' .$singlepost->post_title. '</p>';

and you need the last one, as you can expect.

share|improve this answer
Wonderful detail examples.. – pixelngrain Jan 27 '13 at 17:31
Thanks! Hope it helps all the people like us :) – enam Jan 28 '13 at 16:41
Sure! why not.. – pixelngrain Jan 28 '13 at 16:45
$wpdb->get_row('query', output_type, row_offset);

row_offset (integer) The desired row (0 being the first). Defaults to 0.

va http://codex.wordpress.org/Class_Reference/wpdb

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.