csj Posted July 18, 2008 Share Posted July 18, 2008 I think I need to use something else besides a while loop with this problem I have. I'm just not sure what. 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". And if it does then I need to delete that row from table_1. 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 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. Link to comment https://forums.phpfreaks.com/topic/115385-help-while-loop-or-not/ Share on other sites More sharing options...
awpti Posted July 18, 2008 Share Posted July 18, 2008 Best solution is to pull the contents of both databases into an array and then compare them. You're just over thinking this one. Basically (not tested, general idea): <?php while($row = mysql_fetch_assoc($get_names_res)) { $user_email[] = $row['email']; } unset($row); while($row = mysql_fetch_assoc($get_names2_res)) { $friend_email[] = $row['friend_email']; } $total_user = count($user_email) for($i = 0; $i < $total_user; ++$i) { if(in_array($friend_email[$i], $user_email) { echo 'Found one!'; } else { echo 'Didn't find one!'; } } Link to comment https://forums.phpfreaks.com/topic/115385-help-while-loop-or-not/#findComment-593192 Share on other sites More sharing options...
ratcateme Posted July 18, 2008 Share Posted July 18, 2008 i am not very good with SQL myself but there should be a way to use WHERE and get only the emails mysql can find in both tables. Scott. Link to comment https://forums.phpfreaks.com/topic/115385-help-while-loop-or-not/#findComment-593197 Share on other sites More sharing options...
awpti Posted July 18, 2008 Share Posted July 18, 2008 That's another way, but issues many unnecessary queries. Better to pull it and compare it outside. Doesn't beat so savagely on the DB if it's a large set of data. Link to comment https://forums.phpfreaks.com/topic/115385-help-while-loop-or-not/#findComment-593199 Share on other sites More sharing options...
DoddsAntS Posted July 18, 2008 Share Posted July 18, 2008 Hi, Try something along the lines of SELECT * FROM table2 INNER JOIN table1 on table2.email=table1.email and take a look at Data Join Tutorial Link to comment https://forums.phpfreaks.com/topic/115385-help-while-loop-or-not/#findComment-593207 Share on other sites More sharing options...
csj Posted July 22, 2008 Author Share Posted July 22, 2008 Thanks for the replies!!! With what awpti said, how would I then be able to delete that specific row from my database. The row that matches the email address? Thanks for all the help :D Link to comment https://forums.phpfreaks.com/topic/115385-help-while-loop-or-not/#findComment-596452 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.