使用PHP获取Drupal7中的用户列表

使用PHP获取Drupal7中的用户列表,php,drupal,drupal-7,drupal-modules,Php,Drupal,Drupal 7,Drupal Modules,如何使用PHP从Drupal7数据库中获取用户名 见此: 这是给Drupal6的,我也试过了,但是上面写着 Call to undefined function db_fetch_object() 这是我的密码: $result = db_query("SELECT name AS users_name FROM {users}"); while($row = db_fetch_object($result)) { print_r($row); } 来

如何使用PHP从Drupal7数据库中获取用户名

见此:

这是给Drupal6的,我也试过了,但是上面写着

Call to undefined function db_fetch_object()
这是我的密码:

$result = db_query("SELECT name AS users_name FROM {users}");
while($row = db_fetch_object($result)) {    
        print_r($row);      
}

来自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));
}
注意,在Drupal7中,所有事情都是通过实体发生的。它不是那么快,但是很灵活……

$query=db_select('users','u'); $query ->条件('u.uid',0',) ->字段('u',数组('name')); $result=$query->execute(); 这是Drupal 7数据库api的全部文档

让我们试一试

$query = db_select('users', 'u');
    $query->fields('u', array('name'));
    $result = $query->execute();
    while($record = $result->fetchAssoc()) {
         print_r($record['name']);
    }

如果将while语句改为read,则代码几乎正确

while ($result as $row) 
d7中不再需要db_fetch_对象


这会奏效的。尽管本文中指定的db_select调用可以工作,但它们需要更多的开销,除非您试图生成动态查询,否则应该避免。另请参阅:有关API在d6和d7之间如何变化的信息。如果在此页面上搜索db_fetch_对象,则会提供此信息

在Drupal 7中,您可以使用实体加载获取所有用户

要单独获取用户名,请使用以下命令:

$users = entity_load('user');
$user_names = array();
foreach ($users as $user_id => $user) {
  $user_names[] = $user->name;
}
试试这个:

$query = db_select('users', 'u')
->fields('u')
->execute();
while($result = $query->fetchAssoc()) {
     print_r($record['name']);
}

你也可以试试这个

$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;
         }
    }

从drupal站点获取用户列表的示例代码

$all_users = entity_load('user');
foreach($all_users as $value) {
  $user_list = (array)$value;
  $users[$user_list['uid']] = $user_list['name'];
}
现在,您可以从数组变量$users获得答案

例如,如果要列出除管理员(默认用户)以外的所有用户,请使用以下示例:

$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'];
  }
}
在上述两个示例中,每个用户的id作为数组索引放置,每个usder的名称作为数组值保存。简化示例包括:


Drupal7引入了实体的新概念。 用户现在也包括在实体中。 可以使用以下函数entity_load()获取所有实体的详细信息

要获取数组中的用户名,请执行以下操作:

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

一个更好的方法

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

这将返回所有活动用户,并使用函数中的代码将其用作助手

-1如果有drupal函数,请不要使用数据库查询。或者实际上,此函数会产生问题,它会更改当前用户会话。自动加载另一个用户会话。
$users = entity_load('user');
$usernames = array();
foreach($users as $id => $user){
    $usernames[$user->uid] = $user->name;
}
print_r($usernames);
function GetAllActiveUsers() {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'user')
        ->propertyCondition('status', 1);
  $results = $query->execute();
  $uids = array_keys($results['user']);
  return $uids;
}