Search code examples
phpdrupaldrupal-7drupal-modules

getting list of users in Drupal 7 using PHP


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

Solution

  • 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']);
        }