What potential pitfalls should PHP developers be aware of when dealing with LDAP search results in PHP?

One potential pitfall when dealing with LDAP search results in PHP is not properly handling errors or empty results. To avoid this, developers should always check if the search was successful and if any results were returned before trying to access the data.

// Perform LDAP search
$search = ldap_search($ldap_conn, $base_dn, $filter);
if (!$search) {
    echo "LDAP search failed";
    exit;
}

// Check if any results were returned
$count = ldap_count_entries($ldap_conn, $search);
if ($count == 0) {
    echo "No results found";
    exit;
}

// Process search results
// Fetch and iterate through entries