Skip to content
PHP Logo

How can one effectively handle the returned results from an LDAP search in PHP, and what are the key considerations for processing and displaying the data in a tabular format?

To effectively handle the returned results from an LDAP search in PHP, you can use the ldap_search() function to perform the search and then iterate over the results using ldap_get_entries(). To display the data in a tabular format, you can use HTML table tags and loop through the LDAP entries to populate the table rows.

php
// Perform LDAP search
$ldapconn = ldap_connect("ldap.example.com");
$ldapbind = ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "password");
$ldapresult = ldap_search($ldapconn, "dc=example,dc=com", "(&(objectClass=person)(sn=Smith))");
$ldapentries = ldap_get_entries($ldapconn, $ldapresult);

// Display results in a tabular format
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Email</th></tr>";
for ($i = 0; $i < $ldapentries["count"]; $i++) {
    echo "<tr>";
    echo "<td>" . $ldapentries[$i]["cn"][0] . "</td>";
    echo "<td>" . $ldapentries[$i]["mail"][0] . "</td>";
    echo "</tr>";
}
echo "</table>";

// Close LDAP connection
ldap_close($ldapconn);