使用 PHP 获取 Drupal 7 中的用户列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8455671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting list of users in Drupal 7 using PHP
提问by Bilal
How can I fetch user names from a Drupal 7 database using PHP?
如何使用 PHP 从 Drupal 7 数据库中获取用户名?
See this: Is it possible to use the Drupal api to get a list of users?
请参阅: 是否可以使用 Drupal api 获取用户列表?
This is for Drupal 6 and I have tried this but it says
这是针对 Drupal 6 的,我已经尝试过,但它说
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);
}
回答by Sidra Sultana
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']);
? ?}
回答by Mathankumar
In Drupal 7 you can fetch all the users using entity_load,
在 Drupal 7 中,您可以使用 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;
}
回答by Nikit
user_load_multiple
Example from modules\profile\profile.pages.inc :
user_load_multiple
来自 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...
请注意,在 drupal 7 中,所有事情都通过实体发生。它不是那么快,但很灵活......
回答by Marius Ilie
回答by Dave
Your code was almost correct if you had changed your while statement to read
如果您将 while 语句更改为 read,您的代码几乎是正确的
while ($result as $row)
db_fetch_object is no longer needed in d7
d7 中不再需要 db_fetch_object
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/224333for 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.
它会起作用。尽管本文中指定的 db_select 调用会起作用,但它们需要更多开销,应避免使用,除非您尝试生成动态查询。另请参阅:http: //drupal.org/node/224333,了解有关 apis 如何在 d6 和 d7 之间变化的信息。在此页面上搜索 db_fetch_object 将提供此信息。
回答by Srinivasan.S
My example code to get the list of users from drupal site,
我从 drupal 站点获取用户列表的示例代码,
$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.
现在您可以从数组变量 $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.
在上面的两个例子中,每个用户的 id 被放置为数组索引,每个用户的名称被保存为数组值。
回答by Sandesh Manghale
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.
Drupal 7 引入了新的实体概念。用户现在也包含在实体中。您可以使用以下函数 entity_load() 来获取所有实体的详细信息。
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
谢谢
回答by Ram
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
这将返回所有活动用户并使用函数中的代码将其用作助手
回答by Turtletrail
Simplified example with fetchAll():
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
}
回答by vinox
Try this:
尝试这个:
$query = db_select('users', 'u')
->fields('u')
->execute();
while($result = $query->fetchAssoc()) {
print_r($record['name']);
}