|
-
Mar 5th, 2008, 04:23 AM
#1
Thread Starter
Hyperactive Member
String Mismatch
Hi,
I have a problem. Following is my code. I would like show only those user and email from table1 whose email is not available in table2. But it is not working. I do not find my fault.
PHP Code:
<?php
$link = mysql_connect("localhost","test","test");
mysql_select_db("test",$link);
$tab1 = mysql_query("select * from table1");
$tab2 = mysql_query("select * from table2");
$is_mail_exists = "n";
while($r1 = mysql_fetch_assoc($tab1))
{
$user1 = $r1["name"];
$email1 = $r1["email"];
while($r2 = mysql_fetch_assoc($tab2))
{
$email2 = $r2["email"];
if(strcmp($email1,$email2)==0)
{
$is_mail_exists = "y";
break;
}
}
if($is_mail_exists == "y")
$is_mail_exists = "n";
else
echo $user1.", ".$email1."<br>";
}
?>
Please help. Thank you so much.
-
Mar 5th, 2008, 05:02 AM
#2
Addicted Member
Re: String Mismatch
My first suggestion is stop using * for pulling data from a database. It's ugly and difficult to read what data you have inside.
My other suggestion is that you can do it with one SQL Query and not two separate ones.
Something along the lines of
Code:
SELECT tbl1.username, tbl1.email
FROM table1 as tbl1
JOIN table2 as tbl2
ON tbl1.email <> tbl2.email
WHERE tbl2.email IS NULL;
That might not be perfect but it should point you in the right direction.
-
Mar 11th, 2008, 12:03 AM
#3
Thread Starter
Hyperactive Member
Re: String Mismatch
Thanks a lot JRSofty for the reply. I found it in the following way too,
select * from tab1 where tab1.email not in (select * from tab2);
This is also working fine.
Thank you so much.
-
Mar 11th, 2008, 12:08 AM
#4
Re: String Mismatch
I think the join would be more efficient than subquery, if you care about that.
-
Mar 11th, 2008, 12:09 AM
#5
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|