diff --git a/lib/dmllib.php b/lib/dmllib.php
index eae308b0d46..739bce243b6 100644
--- a/lib/dmllib.php
+++ b/lib/dmllib.php
@@ -816,6 +816,11 @@ function rs_fetch_next_record(&$rs) {
 
     global $CFG;
 
+    if (empty($rs)) {
+        debugging('Incorrect rs used!');
+        return false;
+    }
+
     $rec = false;
     $recarr = $rs->FetchRow(); //Retrieve record as object without advance the pointer. It's quicker that FetchNextObj()