What are the potential pitfalls of querying LDAP data with PHP, and how can they be avoided?

One potential pitfall of querying LDAP data with PHP is not properly sanitizing user input, which can lead to LDAP injection attacks. To avoid this, always use parameterized queries when constructing LDAP queries to prevent malicious input from altering the query structure.

// Avoid LDAP injection by using parameterized queries
$filter = "(uid=" . ldap_escape($username, "", LDAP_ESCAPE_FILTER) . ")";