0

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/>";
        }
    }
}
3
  • 1
    Do these two databases exist in the same server? You could than create a query which spans both by using the full qualified table name. Commented Aug 14, 2010 at 15:37
  • 2
    Do you actually have two databases, or do you just have two tables in the same database? Could you edit to include your sql? As Willi said, it could be much easier than doing these nested loops. Commented Aug 14, 2010 at 15:41
  • Two separate databases, two separate servers. Commented Aug 14, 2010 at 15:57

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.