tags:

views:

373

answers:

3

I would like to get a list of all the users who have been assigned a certain role. I could write my own SQL but I would like to use the api as much as possible.

A: 

Hi,

I don't know about any API can help to collect users by role. Here you are some link you can get something: http://drupal.org/node/82002. Although SO is an awesome service I suggest using drupal.org or #drupal channel on irc.freenode.org for Drupal related questions.

Ps.: sometimes SQL is not that evil:)

I agree. I see a lot of Wordpress plugins who directly query the database instead of using predefined functions.
Chacha102
Just commenting to add that StackOverflow is great for Drupal questions and more people should ask them here.
alxp
sql queries should be avoided, since drupal7 will use new db api in order to abstract SQL (so you can use things like mongodb:)
barraponto
+4  A: 

There are generally no Drupal API functions for this sort of task (pulling up entities that match certain criteria). It tends to focus on single-entity CRUD functions in the API; everything else is up to a developer's own SQL skills.

The Views module allows you to build lists of users filtered by role, permission, etc -- but it could easily be overkill.

Eaton
I would make my own answer but I feel I should just expand on this. There is no function which allows you to list users and filter by role within Drupal (core at least). You can write your own function or like eaton said, use views to create a list of users.
codeincarnate
A views is easy here.
Stewart Robinson
you __really__ should use views. it is the solution you're looking for.
alexanderpas
A: 

One easy option is to use Views to generate the SQL (appears below the view its self when you press the preview button) for you and then use the Drupal SQL abstraction layer to get the results you need if you need to get access to the raw data rather than display a View.

It'd look a bit like this:

$result = db_query('SELECT users.uid AS uid,
   users.mail AS users_mail,
   users.name AS users_name
 FROM users users');
while ($existing_user = db_fetch_object($result)) {
 print_r($existing_user); // or do whatever
  }

Just add more fields to the view to get the complete query.

bloke zero