17.4 Freeing an LDAP Result Set


You want to free the results of an LDAP query so that you don't have large result sets in memory.

Technique

Use the ldap_free_result() function to free result sets:

 <?php // Open and bind a connection $lh = ldap_connect("localhost") or die("Cannot connect to LDAP"); ldap_bind($lh) or die("Cannot Bind"); // criteria $bdn = "o=Sterling's Company, c=US"; $criteria = array("ou"); // perform query and free result $sth = ldap_list($lh, $bdn, "ou=*", $criteria); $data = ldap_get_entries($lh, $sth); ldap_free_result($sth); // loop through results while ($idx < $data["count"]) {     print $data[$idx++]["ou"][0] . "\n<br>\n"; } // close the connection ldap_unbind($lh); ?> 

Comments

The ldap_free_result() function simply takes a handle of a result set and frees the memory allocated internally for that result set. In this example, we search the current level ( ldap_list() ) with our criteria, and then get the results and put them into a variable (which happens to be an array because that is what ldap_get_entries() returns). Then we free the result set because it is no longer needed.

With the advent of PHP4 and Zend's garbage collection feature, ldap_free_result() might seem redundant. However, for backward compatibility and for good programming practices, it is best to free result sets explicitly.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net