39

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?

1
  • 3
    It's called get_row() for a reason...
    – Ciprian
    Commented Aug 17, 2021 at 9:53

4 Answers 4

65

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

1
58

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:

<?php 
$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.

Example:

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

OR

<?php 
$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.

<?php 
global $wpdb;
$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.

3
  • 1
    Wonderful detail examples.. Commented Jan 27, 2013 at 17:31
  • Sure! why not.. Commented Jan 28, 2013 at 16:45
  • 1
    Really great examples. For speed optimisation in last example I will recommend change this: ($allposts as $singlepost) to: ($allposts as $k => $singlepost)
    – Cyborg
    Commented Dec 30, 2020 at 2:39
1
$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

0

my solution is simple..

<?php
function count_results() {
    # use the data base
    global $wpdb;

    # Query to count all results from one table
    $sql_count_results = '
        SELECT count(*) as count
        FROM `YOUR_TABLE`;';

    # Ejecute function
    $results = $wpdb->get_row( $sql_count_results , OBJECT );

    # Return results
    return $results->count;
}

Use:

<?php
echo count_results();
2
  • 4
    It would be great if you can also explain what this code does in addtion to posting it.
    – bravokeyl
    Commented Nov 16, 2016 at 9:21
  • This counts the rows in a table, it is not an answer to the OP's question.
    – alexg
    Commented Jan 15, 2019 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.