views:

42

answers:

1

I have two databases, each containing email addresses of users. I've written a script that has the sole purpose of seeing which email addresses exist in both databases.

With that being said, please look at the following loops. Each loop contains hundreds of records (I've printed out the results from each as a verification). However, when the conditional finds a match, the conditional only matches against the email address from the first match. It remains "stuck" on the one record it matched from the initial query. In other words, if it matched on "[email protected]", then all future comparisons are against "[email protected]". It's almost like I need a "next()" method to move things along from the first array.

FWIW, I'm a complete PHP noob, so I'm sure there's something obvious I'm missing here.

while($member = mysql_fetch_array($members)){
    while($emailAddress = mysql_fetch_array($emailAddresses)){
        if ($member['email'] == $emailAddress['email']){
            echo $member['email'] . "<br/>";
        }
    }
}
+2  A: 

I think the problem is you looping through a mysql-result set, there's an internal data pointer that keeps sliding up 1 step at each loop.

Try putting the results in an array first.

$adresses = array();
while($emailAddress = mysql_fetch_array($emailAddresses)){
    $adresses[] = $emailAddress['email'] ; 
    }

Then check your result within the array

while($member = mysql_fetch_array($members)){
   if(in_array($member['email'], $adresses)){
     echo $member['email'];
     }
   }

Or perhaps you can reset the datapointer after each loop, not sure if you can do this on mysql results but it works when looping through arrays.

Rakward
Perfect! A+ for you.
Huuuze