How do I compare the results of 2 different queries?

I am trying to compare the results of 2 different types of queries.

the first query is a ldap query, looking for accounts which are disabled.
gathering the following : (displayname, mail, descriptions, physicalofficename)

the second query is a sql query against the bes database looking for active bes users
gathering the following : (displayname, mailboxsmtpaddr, status, lastsenttime)

Essentially - I want to evaluate both queries, and determine by either matching the displayname or email address, so we can determine accounts which are disabled on either end.

I get both individual queries working perfectly, and I'm thinking I simply need to create an array with the 2 different queries, and then intersect to find the unique data.

However, I'm a little fuzzy on how best to do this with 2 dissimalar queries..

Could anyone give any insight of how to do this?

Thank you all for your help!
Chris


LDAP PHP PAGE
 
// Specify only those parameters we're interested in displaying
$attrs = array("displayname","mail","description","physicaldeliveryofficename");

// Create the filter from the search parameters
//$filter = $_POST['filter']."=".$_POST['keyword']."*";
$filter = "userAccountControl=546";

$search = ldap_search($ad, $dn, $filter, $attrs)
          or die ("ldap search failed");

$entries = ldap_get_entries($ad, $search);

// function from http://php.net/manual/en/function.ldap-sort.php //
function sort_ldap_entries($e, $fld, $order) 
{ 
    for ($i = 0; $i < $e['count']; $i++) { 
        for ($j = $i; $j < $e['count']; $j++) { 
            $d = strcasecmp($e[$i][$fld][0], $e[$j][$fld][0]); 
            switch ($order) { 
            case 'A': 
                if ($d > 0) 
                    swap($e, $i, $j); 
                break; 
            case 'D': 
                if ($d < 0) 
                    swap($e, $i, $j); 
                break; 
            } 
        } 
    } 
    return ($e); 
} 

function swap(&$ary, $i, $j) 
{ 
    $temp = $ary[$i]; 
    $ary[$i] = $ary[$j]; 
    $ary[$j] = $temp; 
} 
?> 
<html>
<body>
<br /><br />
<?php 
    $entries = sort_ldap_entries($entries, 'displayname', 'A'); // sort entries by ascending order of mail 
?> 
<table border="1px" style="border-collapse:collapse; border:solid 1px #666; font-size:12px;" width="100%">
    <tr>
        <td><strong>Name</strong></td>
        <td><strong>Email</strong></td>
        <td><strong>Description</strong></td>
        <td><strong>PhysicalOffice</strong></td>
    </tr>
	<?php
    for ($i=0; $i<$entries["count"]; $i++)
    {
        echo "<tr><td>" . $entries[$i]["displayname"]
             [0] . "</td>" . "<td>" . $entries[$i]["mail"][0] . "</td>" . "<td>" . $entries[$i]["description"][0] . "</td>" . "<td>" . $entries[$i]["physicaldeliveryofficename"][0] . "</td></tr>";
    }
?>
</table>
</body>
</html>
<?php
    ldap_unbind($ad);
?>

Open in new window


SQL PHP PAGE
 
<?php 
include ("db/bes_db_connect.php");
$query = "SELECT UserConfig.Id, UserConfig.DisplayName, UserConfig.MailboxSMTPAddr, UserStats.UserConfigId, UserStats.Status, UserStats.LastSentTime ";
$query .= "FROM UserConfig, UserStats ";
$query .= "WHERE UserStats.Status like '%12%' AND UserStats.UserConfigId = UserConfig.Id"; 

//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result); 
?>
<html>
<body>
<?php
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 
//display the results
?>
<br /><br />
<table border="1px" style="border-collapse:collapse; border:solid 1px #666; font-size:12px;" width="100%">
<tr>
<td><strong>DisplayName</strong></td>
<td><strong>MailboxSMTPAddr</strong></td>
<td><strong>Status</strong></td>
<td><strong>LastSentTime</strong></td>
</tr>
<?php
while($row = mssql_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . $row["DisplayName"] . " </td><td> " . $row["MailboxSMTPAddr"] . " </td><td> " . $row["Status"] . " </td><td> " . $row["LastSentTime"] . "</td>";
  echo "</tr>";
}
//close the connection
mssql_close($dbhandle);
?>
</table>
<br /><br />
</body>
</html>

Open in new window

LVL 2
udsfsgAsked:
Who is Participating?
 
Hugh McCurdyConnect With a Mentor Commented:
Will this work for you?

	} else {
		echo "No matching records found..";
                break;
	}

Open in new window

0
 
ghodderCommented:
So your LDAP query only returns disabled accounts?

Basically run your LDAP query and get the results into an array, run your SQL query and get the results into an array, then loop over the LDAP results and check whether the displayname exists in the SQL array.


Some pseudo-code might be
$entries = ldap_get_entries($ad, $search);
$result = mssql_query($query);
$sql_result = array();

foreach (mssql_fetch_row($result) as $row) {
    $sql_result[] = $row;
}

foreach ($entries as $row) {
    if (in_array($row['displayname'][0], $sql_result)) {
        // Do whatever
    }
}

Open in new window

0
 
Hugh McCurdyCommented:
Do you still need help?  If so, more information would help.  It sounds like you want a report (?) of all accounts that are disabled (at either end).  I would guess that you don't want the same account showing up on the report twice.  This implies some sort of sort.  However, at this point I'm doing a lot of guessing about what you really want.  (Nor do I know if you've solved the problem already.)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
udsfsgAuthor Commented:
ghodder, That is exactly what I was looking for.
Thank you very much sir.

I do have 1 small issue, everything appears to be working, and I have verified, I don't have erronious entries, however, my loop appears to repeat even after adding an else statement. Is there a better way to terminate the loop to have a little cleaner ending?

 looping

<?php
$entries = ldap_get_entries($ad, $search);
$result = mssql_query($query);

$sql_result = array();  
?>
<h2>Deactivated AD accounting matching an Active BES account.</h2>
<p>This table should remain empty if all accounts have been properly terminated. </p>
<?php
foreach (mssql_fetch_row($result) as $row) {  
	$sql_result[] = $row;  
}  
foreach ($entries as $row) {  
	if (in_array($entries['mail'], $sql_result)) {  
		echo "Match found!:" .$entries['mail'] . " AND " . $row['MailboxSMTPAddr'] . "located in both AD and BES.";
	} else {
		echo "No matching records found..";
	}
} 
//close the connection
ldap_unbind($ad);
mssql_close($dbhandle);
?>

Open in new window

0
 
udsfsgAuthor Commented:
Thank you very much for the help.
0
 
Hugh McCurdyCommented:
Glad to have been of assistance.  Thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.