By drupalworld on
I found this great snippet to count how many people have registered an account on my site:
<?php
$sql = 'SELECT COUNT(uid) as count FROM {users} where status = 1';
$count = db_result(db_query($sql));
print $count;
?>
But I would like to know if there is way to count how many registered users from a specific role?
Many thanks,
Drupalworld
Comments
Try this
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
script not working
this script is not working, any idea whats missing?
grandmaster
Hmm...
Did you close out the PHP code? This worked for me when I wrote it.
NancyDru
error comes up!
yes, i closed out the php code, this is the error which shows up:
user warning: Column 'uid' in field list is ambiguous query: eval SELECT COUNT(uid) as count FROM users u INNER JOIN users_roles ur ON u.uid=ur.uid WHERE ur.rid = 3 AND u.status = 1 in .../includes/database.mysql.inc on line 172.
user warning: Query was empty query: eval in .../includes/database.mysql.inc on line 172.
here's the code:
~ grandmaster
Because it is
Changing
COUNT(uid)toCOUNT(u.uid)should solve the problem.Still having error
I changed COUNT(uid) to COUNT(u.uid) and I'm still getting error messages:
"user warning: Query was empty query: in /home/nydac02/public_html/includes/common.inc(1547) : eval()'d code on line 6."
I'm trying to display the number of users that belong to a specific role.
Thanks!
Evan
I think print
I think
should be
That worked, thanks!!
That worked, thanks!!
If you want the count for all roles this may help
This snippet will show the count of registered users (uid > 0) that are active (status = 1) per role (as long as there is at least one registered user for the role). Note that if you allow/have users with more than one role, the sum of the counts will be greater than the number of users.
Thanks both of you for these
Thanks both of you for these snippets :)
Very useful indeed!
Drupalworld
According to the handbook
I should ask: how do I use this snippet? Can I - after making sure that the input format is right - just paste this in a block and put that on my user page? Of course I could experiment with it, but since this can have unforeseen consequences I'd rather inquire first.
Thx
TR
Correct, you can use the
Correct, you can use the snippet in the way you described.
Yes I have it working now
One tip for whoever tries this as well: If you use a prefix in your database, do not put this prefix before the table names in the above code. Drupal takes care of that.
Thanks
Would there be a way
to show the number of blocked users as well?
Thanks
Simple
Simply repeat the query with
WHERE u.status = 0.NancyDru
NancyDru
That works, great, but
what i do not understand is that only the custom roles are reported, not the built-in role of the plain registered user. Can anything be done about that?
Thanks
question withdrawn, this thread starts with the solution Sorry