View Full Version : PHP [help] WHILE loop not working??
cjj
July 17th, 2008, 03:53 AM
I'm trying to run a while loop so I can determine if one of the (many) email addresses inside my "table_1" match any of the email addresses inside my "table_2".
But it won't work!!
I must be doing something wrong or maybe I shouldn't even be using a WHILE loop. Can someone help me with this. I would be so happy and grateful!! :D :D
while($row2 = mysql_fetch_array($get_names2_res)) {
while($row = mysql_fetch_array($get_names_res)) {
$friend_email = stripslashes($row['friend_email']);
$users_email = stripslashes($row2['email']);
if ($users_email == $friend_email) {
echo"Yeah we found that name ($users_email)";
}
}
}
It works but not how I want it to work. It only reads one email at a time and I need it to read all the emails at one time.
agnus
July 17th, 2008, 05:46 AM
You should do this from SQL.
SELECT COUNT(*) AS total FROM my_table WHERE friend_email NOT IN ( SELECT email FROM my_table )
This will tell you the number of friend e-mails that are different from the senders e-mails.
cjj
July 17th, 2008, 06:01 AM
Thanks for the reply. :D The problem is that I don't need a number of emails, I need to know if a specific email address is inside both MYSQL tables.
agnus
July 17th, 2008, 06:04 AM
Sorry, my bad. Try this and let me know if it works
SELECT friend_e_mail FROM my_table WHERE friend_email NOT IN ( SELECT email FROM my_table )
A little note. This SQL checks on the same table if the e-mails in friend columns are also in e-mail column. If you have 2 separate tables, do it like this:
SELECT friend_e_mail FROM my_table WHERE friend_email NOT IN ( SELECT email FROM my__other_table )
Later edit
I realized now that you need to see the duplicates. To do so, change NOT IN in simply IN. The above SQL's will show you the unique friend emails.
cjj
July 17th, 2008, 07:26 AM
No that doesn't work. But thanks for trying to help me :D
Charleh
July 17th, 2008, 07:40 AM
No that doesn't work. But thanks for trying to help me :D
agnus' solution should work fine - I use SQL for a living, this is not a complex query
SELECT EmailAddress FROM emails WHERE EmailAddress IN (SELECT EmailAddress FROM otheremails)
Will give you all email addresses from the 'emails' table which have an email address in the 'otheremails' table
Just substitute your field names/table names in
Most data manipulation is easily done in SQL if you know how. It saves processing at the PHP end and means you don't need to recycle PHP code if you make it a stored procedure
cjj
July 17th, 2008, 08:03 AM
I did a query like this.
$get_names = "SELECT email FROM refer_names WHERE email IN ( SELECT email FROM users_referal)";
$get_names_res = mysql_query($get_names) or die (mysql_error());
Is that correct? Then how do I retrieve the info? With a while loop?
Thanks :D :D
agnus
July 17th, 2008, 12:05 PM
Yeap, you do the while and you'll have all the duplicates.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.