Drupal database: how to join 2 tables?
To check user role before login you need to create a validate function for login. You can put this code in template.php of your theme or in custom module file. e.g my custom module name is MYMODULE. 

&lt;?php

/**
* Implements hook_form_alter().
*/
function MYMODULE_form_alter(&amp;$form, &amp;$form_state, $form_id) {
if ($form_id == 'user_login') {
$form['#validate'][] = 'MYMODULE_user_login_validate';
}
}

/**
* Additional handler for user_login form validate.
*/
function MYMODULE_user_login_validate($form, &amp;$form_state) {
  // check user role here by the function mentioned abve
   $name = $form_state['values']['name'];
   $user = user_load_by_name($name);
   $role = array_key_exists(2, $user-&gt;roles);
   // do something
}


Hope it will help you.