You can't make generic code, as the what Drupal use is the role id (rid) of a role, which is a serial.
A users roles, is in the user_roles
table containing uid and rid, so to remove a role you would do:
global $user;
$rid = x; // x = the id of the role to remove
db_query("DELETE FROM {user_roles} WHERE uid = %d AND rid = %d", $user->uid, $rid);
To give the new role you could do:
$record = array(
'uid' => $user->uid,
'rid' => y, // y = the id of the role to give.
);
drupal_write_record('user_roles', $record);
googletorp
2010-05-03 10:11:40