Lightweight way to check to see if another user has a role There are lots of examples of seeing if the current user has a particular role. I need lightweight function to see if another user has a particular role.

For comments, I want to highlight comments from users who have a particular role, "Guest Blogger". Using hook_preprocess_comment(&amp;$variables) I can load up each user using:

$user_fields = user_load($variables['comment']-&gt;uid);


But this seems rather heavy. All I need to do is see if the user has a role, not load up the entire user object.

Can someone help me out with a function or a query where I pass in the uid and the function/query passes back an array of roles?
Something like:

function MYMODULE_get_user_roles($uid) {
  $roles = &amp;drupal_static(__FUNCTION__, array());

  if (!isset($roles[$uid])) {
    $query = db_select('role', 'r')
      -&gt;fields('r', array('rid', 'name'))
      -&gt;condition('ur.uid', $uid);

    $query-&gt;join('users_roles', 'ur', 'ur.rid = r.rid');

    $roles[$uid] = $query-&gt;execute()-&gt;fetchAllKeyed();
  }

  return $roles[$uid];
}


You might want to add static caching to that if you plan to call it more than once for any single user during a single page build.