function _drush_user_get_uid

6.x user.drush.inc _drush_user_get_uid($search)
4.x user.drush.inc _drush_user_get_uid($search)
5.x user.drush.inc _drush_user_get_uid($search)

Get uid(s) from a uid, user name, or email address. Returns a uid, or FALSE if none found.

1 call to _drush_user_get_uid()
_drush_user_get_users_from_arguments in commands/user/user.drush.inc
Given a comma-separated list of users, return uids for users that match either by uid or email address.

File

commands/user/user.drush.inc, line 599
Drush User Management commands

Code

function _drush_user_get_uid($search) {
  // We use a DB query while looking for the uid to keep things speedy.
  $uids = array();
  if (is_numeric($search)) {
    if (drush_drupal_major_version() >= 7) {
      $uid_query = db_query("SELECT uid, name FROM {users} WHERE uid = :uid OR name = :name", array(':uid' => $search, ':name' => $search));
    }
    else {
      $uid_query = db_query("SELECT uid, name FROM {users} WHERE uid = %d OR name = '%d'", $search, $search);
    }
  }
  else {
    if (drush_drupal_major_version() >= 7) {
      $uid_query = db_query("SELECT uid, name FROM {users} WHERE mail = :mail OR name = :name", array(':mail' => $search, ':name' => $search));
    }
    else {
      $uid_query = db_query("SELECT uid, name FROM {users} WHERE mail = '%s' OR name = '%s'", $search, $search);
    }
  }
  while ($uid = drush_db_fetch_object($uid_query)) {
    $uids[$uid->uid] = $uid->name;
  }
  switch (count($uids)) {
    case 0:
      return drush_set_error("Could not find a uid for the search term '" . $search . "'!");
      break;
    case 1:
      return array_pop(array_keys($uids));
      break;
    default:
      drush_print('More than one user account was found for the search string "' . $search . '".');
      return (drush_choice($uids, 'Please choose a name:', '!value (uid=!key)'));
  }
}