Results 1 to 5 of 5

Thread: String Mismatch

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question 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.

  2. #2
    Addicted Member JRSofty's Avatar
    Join Date
    Jan 2004
    Location
    Somewhere in Germany
    Posts
    149

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Thumbs up 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.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: String Mismatch

    I think the join would be more efficient than subquery, if you care about that.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: String Mismatch


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width