diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php
index cb8e1c64bb..8010a857ca 100644
--- a/phpBB/includes/functions_user.php
+++ b/phpBB/includes/functions_user.php
@@ -1045,13 +1045,15 @@ function user_ban($mode, $ban, $ban_len, $ban_len_other, $ban_exclude, $ban_reas
 					$banlist_ary[] = (int) $row['user_id'];
 				}
 				while ($row = $db->sql_fetchrow($result));
+
+				$db->sql_freeresult($result);
 			}
 			else
 			{
 				$db->sql_freeresult($result);
+
 				trigger_error('NO_USERS', E_USER_WARNING);
 			}
-			$db->sql_freeresult($result);
 		break;
 
 		case 'ip':