diff --git a/include/class_ldap.inc b/include/class_ldap.inc
index f1bc05ec5c9051786cd13d8c221b330fbf3f3897..b6b74c276b5fb599a027842f0951b8985d285fd3 100755
--- a/include/class_ldap.inc
+++ b/include/class_ldap.inc
@@ -604,10 +604,17 @@ class LDAP
    *
    * \param $srp srp
    */
-  function count ($srp)
+  function count($srp)
   {
     if ($this->hascon) {
-      if ($this->hasres[$srp] && !is_bool($this->hasres[$srp])) {
+      if (!empty($this->hasres[$srp])) {
+        // Ensure $this->sr[$srp] is a valid LDAP result
+        if ($this->sr[$srp] === false) {
+          $this->error = "LDAP search returned no results";
+          logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'count()');
+          return 0; // Return 0 as there are no entries
+        }
+
         $rv = @ldap_count_entries($this->cid, $this->sr[$srp]);
         $this->error = @ldap_error($this->cid);
         logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'count()');
@@ -624,7 +631,6 @@ class LDAP
     }
   }
 
-
   /*!
    * \brief Remove
    *