diff --git a/auth/db/auth.php b/auth/db/auth.php
index <HASH>..<HASH> 100644
--- a/auth/db/auth.php
+++ b/auth/db/auth.php
@@ -334,10 +334,12 @@ class auth_plugin_db extends auth_plugin_base {
 
         // simplify down to usernames
         $usernames = array();
-        foreach ($users as $user) {
-            array_push($usernames, $user->username);
+        if (!empty($users)) {
+            foreach ($users as $user) {
+                array_push($usernames, $user->username);
+            }
+            unset($users);
         }
-        unset($users);
 
         $add_users = array_diff($userlist, $usernames);
         unset($usernames);
Merged from MOODLE_<I>_STABLE: MDL-<I> - auth/db - suppress php warning when there are no users using db auth