3

How can I fetch user names from a Drupal 7 database using PHP?

See this: Is it possible to use the Drupal api to get a list of users?

This is for Drupal 6 and I have tried this but it says

Call to undefined function db_fetch_object()

Here is my code:

$result = db_query("SELECT name AS users_name FROM {users}");
while($row = db_fetch_object($result)) { 
 print_r($row); 
}
asked Dec 10, 2011 at 9:50

11 Answers 11

8

Lets give it a try

$query = db_select('users', 'u');
  $query->fields('u', array('name'));
  $result = $query->execute();
  while($record = $result->fetchAssoc()) {
     print_r($record['name']);
  }
answered Dec 10, 2011 at 10:13
1
5

In Drupal 7 you can fetch all the users using entity_load,

To get the user names alone use the following,

$users = entity_load('user');
$user_names = array();
foreach ($users as $user_id => $user) {
 $user_names[] = $user->name;
}
answered Jun 24, 2013 at 18:45
1
  • Actually this function creates an issue, it changes current user session. Loads another user session automatically. Commented Oct 17, 2014 at 11:12
4

user_load_multiple
Example from modules\profile\profile.pages.inc :

$users = user_load_multiple($uids);
$content = '';
foreach ($users as $account) {
 $profile = _profile_update_user_fields($fields, $account);
 $content .= theme('profile_listing', array('account' => $account, 'fields' => $profile));
}

Note that in drupal 7 all things happen via entity. It's not so fast, but flexible...

answered Dec 10, 2011 at 10:08
2
$query = db_select('users', 'u');
$query
 ->condition('u.uid', 0, '<>')
 ->fields('u', array('name'));
$result = $query->execute();

Here's the whole documentation for Drupal 7 Database api

answered Dec 10, 2011 at 10:11
1

Your code was almost correct if you had changed your while statement to read

while ($result as $row) 

db_fetch_object is no longer needed in d7

it would have worked. Although db_select calls specified in this post will work, they require more overhead and should be avoided unless you are trying to generate dynamic queries. Also see: http://drupal.org/node/224333 for info on how the apis have chaned between d6 and d7. A search for db_fetch_object on this page would've given this info.

answered Dec 10, 2011 at 15:55
1

My example code to get the list of users from drupal site,

$all_users = entity_load('user');
foreach($all_users as $value) {
 $user_list = (array)$value;
 $users[$user_list['uid']] = $user_list['name'];
}

Now you can get the answer from the array variable $users.

For example if you want to list all the user other than administrator (default user) means use the below example:

$all_users = entity_load('user');
foreach($all_users as $value) {
 $user_list = (array)$value;
 if($user_list['uid'] > 1) {
 $users[$user_list['uid']] = $user_list['name'];
 }
}

In the above both example, the id of each user is placed as array index and the name of each usder is saved as array value.

answered Nov 22, 2013 at 13:50
1

Drupal 7 introduces a new concept of Entity. User also included in Entity now. You can use following function entity_load() to get the details of all entities.

To fetch the usernames in array :

$users = entity_load('user');
$usernames = array();
foreach($users as $id => $user){
 $usernames[$user->uid] = $user->name;
}
print_r($usernames);

Thanks

answered Nov 24, 2014 at 8:09
0

A Better way

function GetAllActiveUsers() {
 $query = new EntityFieldQuery();
 $query->entityCondition('entity_type', 'user')
 ->propertyCondition('status', 1);
 $results = $query->execute();
 $uids = array_keys($results['user']);
 return $uids;
}

This will return all active users and use the code in a function to use it as a helper

answered Sep 25, 2018 at 18:18
-1

Try this:

$query = db_select('users', 'u')
->fields('u')
->execute();
while($result = $query->fetchAssoc()) {
 print_r($record['name']);
}
answered Jun 25, 2013 at 3:33
-1

You can try this as well,

$query = db_select('users', 'u');
 $query->fields('u', array('uid, name'));
 $result = $query->execute();
 while($record = $result->fetchAssoc()) {
 $account = user_load($record['uid']);
 if($account->uid){
 print $account->name;
 }
 }
answered Aug 29, 2013 at 11:31
-1

Simplified example with fetchAll():

 $users = db_select('users', 'u')
 ->fields('u', array('uid'))
 ->execute()
 ->fetchAll();
 foreach ($users as $user) {
 $full_user = user_load($user->uid);
 //do your stuff here 
 }
answered Feb 8, 2014 at 13:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.