Example code from my ldap class.
this->query i send notes under ldap_search function documentation.
public function queryUserLogin($user_name='', $user_psw='', $tls_bool=true) // na localhostu pouzit tls=false
    {
    var_dump('queryUserLogin');
//$tls_bool = false;    // for localhost
    $fn     = 'queryUser';
    $config = $this->config;
    $conn   = $this->conn;
    if (!$conn)
        {return $this->errorConnection($fn);}
    // user - exist name?
    $user_name = $this->escapeValue($user_name);
    $user_psw  = $this->escapeValue($user_psw);
    $filter = "(&(objectclass=person)(cn=".$user_name."))";
//var_dump($filter);
    $att    = array('dn');
    $data   = $this->query($filter, $att, 2, true);
    if ($data===false)
        {
        $this->error("User error, nenalezen v ldap!");
        $this->disconnect();
        return false;
        }
    // user - exist user in LDAP (name, psw)
    $user = is_array($data) && isset($data[0]) && isset($data[0]['dn']) ? $data[0]['dn'] : '';    // "cn=mynickname,ou=users,o=su" 
    $psw  = $user_psw;
    if ($user=='' || $psw=='')
        {
        $this->error("User error, prazdne prihlasovaci udaje!");
        $this->disconnect();
        return false;
        }
    if ($tls_bool==true)
        {
        if (ldap_start_tls($conn))    //php7
            {
            $result = @ldap_bind($conn, $user, $psw);
            }
        else    {
            $this->error("Unable to start TLS!");  // SSL encrypt protocol, certificate, deprecated?
            }
        }
    else    {
        $result = @ldap_bind($conn, $user, $psw);
        }
    if ($result==false)
        {
        $this->error("Bind user error! bindDN = ".$user);    //".ldap_error($conn)."
        $this->disconnect();
        return false;
        }
    // user - get user info, bind to ldap user with more permitions and get data (login to intercon user)
    $user   = $config['userInterconDn'];
    $psw    = $config['userInterconPsw'];
    if ($tls_bool==true)
        {
        if (ldap_start_tls($conn))    //php7
            {
            $result = ldap_bind($conn, $user, $psw);
            }
        else    {
            $this->error("Unable to start TLS!");  // SSL encrypt protocol, certificate, deprecated?
            }
        }
    else    {
        $result = ldap_bind($conn, $user, $psw);
        }
    if ($result==false)
        {
        $this->error("Bind user intercon error!");    // . ldap_error($conn) !Nezobrazovat jmeno usera interconu
        $this->out = array(
            'uid'         => $user
            );
        return true;
        }
    // search user data
    $filter = "(&(objectclass=person)(cn=" . $user_name . "))";
//var_dump($filter);
    $att    = array(
        'uid', 
        'workforceID',
        'employeeID',
        'givenName',
        'sn',
        'mail'
        );
    $data   = $this->query($filter, $att, 2, true);
       // get data from ldap entires structure
    $this->out = array(
        'uid'         => $this->dataGetValue($data, 'uid'        , ''),
        'workforceID' => $this->dataGetValue($data, 'workforceid', ''),    
        'employeeID'  => $this->dataGetValue($data, 'employeeid' , ''),    
        'givenName'   => $this->dataGetValue($data, 'givenname'  , ''),
        'sn'          => $this->dataGetValue($data, 'sn'         , ''),    // prijmeni
        'mail'        => $this->dataGetValue($data, 'mail'       , '')    // mail
        );
    $this->conn = $conn;
    return true;
    }