-
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.
-
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.
-
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.
-
Re: String Mismatch
I think the join would be more efficient than subquery, if you care about that.
-
Re: String Mismatch