Currently I'm learning drupal database command as I'm pretty new to Drupal, I'm more familiar with mysqli api. I want to check the role of user before they are logged in to the system, the role table maps rid
with a role (for example 1 - admin, 2 - author, etc.), the users
table and users_roles
table are linked by uid
, which is unique. The role i want to verify before user login to the system has rid (role id) = 2. This is what I have so far, but it doesn't really work:
function is_author($name) {
$query = db_select('users_roles');
$query->join('users', 'users_roles.uid = users.uid'); // JOIN users_roles with users
$query->fields('users_roles', array('rid')) // SELLECT the field rid from users_roles
->fields('users', array('name')) // SELLECT the field name from users
->condition('users.name', ':name');
$result = $query->execute();
while ($record = $result->fetchAssoc()) {
if ($record == 2) {
break;
}else{
return false;
}
}
return true;
}
This is the users and users_roles table:
table users
:
uid | name | pass | .....
1 | Michael | (encrypted password)
2 | Ted | (encrypted password)
3 | Mark | (encrypted password)
Table users_roles
:
uid | rid
1 | 2
2 | 3
Please help!! Thanks in advanced!
$record == 2
is not going to work. Try with fetchCol(), by checking the$result
array.