I need to get the total number of users in the database and throw it into a block.

All I need is the number - I can manipulate from there. If there are 200 members, I need it to return '200'.

Thanks.

Comments

vm’s picture

http://drupal.org/node/66638

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

CompShack’s picture

I have made this block quickly just to get you started!

$result = db_query("SELECT COUNT(0) AS num FROM {users} u");
while ($blog = db_fetch_object($result)) {
print '<ul><li>'.$blog->num .'</li></ul>';
}
print '</table>';

-----------------------------------------
Finally, I CMS that I Like!
http://www.CompShack.com

Damion’s picture

Thanks for the snippet.
It looks like it is pulling the "Anon" user as well. I have 5 users and script is showing 6. This will work perfectly for my needs though - just take total users - 1 and I can run my other calculations off that.

Thanks!

CompShack’s picture

If i'm not mistaken, Anon users have a value of 0. so if you do the following it should work.

$result = db_query("SELECT COUNT(0) AS num FROM {users} u where u.uid > 0");
while ($blog = db_fetch_object($result)) {
print '<ul><li>'.$blog->num .'</li></ul>';
}
print '</table>';

I just added the where u.uid > 0 :)
-----------------------------------------
Finally, I CMS that I Like!
http://www.CompShack.com

Damion’s picture

I appreciate that. I started thinking about that after I subtracted 1 from result, and actually put the code verbatim on my site what you rewrote - just forgot to post back here :)

Thanks again for your help. I think with everyone's help here I may actually be able to 'get' the inner-workings of Drupal.
The more I learn about it- the more I like ;)

petermilad’s picture

Thanks a lot CompShack this is exactly what I was searching for.

Peter Aziz

lannygoodman’s picture

this is great, but i don't know where to put the code. i'm using 6.16 and as far as i can tell code for the "who's online" and "online users for the forum (where i'm using this) is found in users.module. i want to put the total registered users between the two, but if i lift the code out from between the and tags, the page crashes (whiteout). any suggestions would be most appreciated!

lanny goodman

darrenmothersele’s picture

You don't need the while loop - just use db_result() instead of db_fetch_object().

wjackson’s picture

Thanks CompShack! This is exactly what I needed, and with the Custom Field Module I was able to integrate this right into views as a custom php field. I would also like to note that with this code, the number that is generated is unformatted. By simply changing "$blog->num" into "number_format($blog->num)" I was able to add commas. Thanks again!

DooDoo’s picture

has anyone a work code for Drupal 7 ?

The later code has an error.

jghyde’s picture

<?php
$members = db_query("SELECT count(uid) FROM {users}")->fetchField();
?>

Local News Platform Built on Drupal
http://sanangelolive.com/

lannygoodman’s picture

hi,
i'm not a php programmer. i set up a new block, pasted this code into it, labeled it php code but it doesn't show up. any suggestions? thanks.
lanny

vm’s picture

labeled it php code

what do you mean by you labeled it? Are you using the php text format?

thomas1977’s picture

Just confirming: indeed, this php code doesn't seem to generate the desired info in the block (in fact, the block doesn't even seem to show up)

gUtto’s picture

DRUPAL 7: I put this code into a new block

$members = db_query("SELECT count(uid) FROM {users}")->fetchField();
echo "There are $members registered users";

and it worked fine.

The block has to have "php code" as input text format
Ensure you have the php filter module enabled!

byebye
A

vm’s picture

Rather than placing that directly into a new block which can cause issues later when upgrading the site ... it is better to place it in a custom block module.

a scaffold for a custom block module can be found in the examples module http://drupal.org/project/examples

rteijeiro’s picture

Hi, here you are the code that creates a block with the number of active users of a Drupal website:

This if the function that returns the number of users

/**
 * Get users count
 * @returns: Number of users active on the site
 */
function cancamusic_get_users_count() {
  $query = db_select('users', 'u');
  $query->fields('u', array('uid'));
  $query->condition('status', 1, '=');
  $users = $query->execute();

  return $users->rowCount();
}

And this is the functions that creates the block


/**
 * Implements hook_block_info()
 */
function cancamusic_block_info() {
  $blocks['cancamusic'] = array(
    'info' => t('Cancamusic Block'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view()
 */
function cancamusic_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'cancamusic':
      $block['subject'] = 'Active Users';
      $block['content'] = cancamusic_get_users_count();
      break;
  }
  return $block;
}

Hope it helps.

Ruben Teijeiro
Drupal Hero