How do I get all the users in an organic group with specific role programatically? I'm trying to use rules to send an email to all users with a specific role in an organic group when someone requests to join the group.

This code almost works. It returns all users from a specific group ($GID) with the specified role ($RID), but it doesn't restrict the role to that group. In other words, if they have the appropriate role in another group, they're included in the results. 

$current_members = db_select('og_membership', 'om');
$current_members-&gt;join('og_users_roles', 'ogur', 'om.etid = ogur.uid');
$current_members-&gt;condition('om.gid', $GID);
$current_members-&gt;condition('om.entity_type', 'user');
$current_members-&gt;condition('ogur.rid', $RID); 

$result = $current_members-&gt;execute();
while ($res = $result-&gt;fetchAssoc()) { $members[] = $res['etid']; }

// Remove duplicate items.
$members = array_keys(array_flip($members));


What do I need to change to restrict the results so that it only returns users with the specified role ID within the specified group?
It's just a simple sql query, no joins needed:

ie. select uid from og_users_roles where rid=3 and gid=835;

  $members = array();
  $current_members = db_select('og_users_roles', 'ogur');
  $current_members-&gt;fields('ogur', array('uid'));
  $current_members-&gt;condition('ogur.rid', $rid);
  $current_members-&gt;condition('ogur.gid', $gid);

  $result = $current_members-&gt;execute();
  while ($res = $result-&gt;fetchAssoc()) { $members[] = $res['uid'];}

  // Remove duplicate items.
  $members = array_keys(array_flip($members));