1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with multiple bd query

Discussion in 'PHP' started by gruntre, Mar 12, 2005.

  1. #1
    I have a small script working but I would like to change it slightly and I just can't fugure out how to do . I'm sure this is a simple thing but my hours of trial and error have only led to failure and frustration. Here is the working version:
    include("config.php"); 
    // Connect to the database server 
    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database $DBName"); 
    mysql_select_db($DBName) or die("Unable to select database $DBName"); 
    ?> 
    <p> Here are all the email address in our orders database: 
    </p> 
    <blockquote> 
    <?php 
    $yes = "Yes subscribe me"; 
    $result = @mysql_query("SELECT email FROM 
    orders WHERE edata = '$yes'"); 
    if (!$result) { 
    echo("<p>Error performing query: " . mysql_error() . 
    "</p>"); 
    exit(); 
    } 
    while ( $row = mysql_fetch_array($result) ) { 
    echo($row["email"] . "<br>"); 
    } 
    ?> 
    PHP:
    What I would like to do is query a different db and then echo output a list of only the email adresses from the first query that do not appear on the second query (ie. only selected emails from new orders who are not already confirmed subcribers) I have the second query working but I have no idea how to do the comparision and print the resulting list. Here is thesecond query:
    <?php 
    include("config.php"); 
    
    <?php 
    
    // Connect to the 2nd database 
    mysql_select_db($DB2Name) or die("Unable to select database $DBName"); 
    $result2 = @mysql_query("SELECT email FROM phplist_user_user WHERE confirmed = '$1'"); 
    /* How do I make a statement that will compare $Result and $result2 and loop and output until all the adresses on $result that are not on $result2 are all displayed in a list?*/ 
    ?> 
    PHP:
    I'm sure it's possible to query 2 different dbs at the same time but it is a little advanced for me. I don't mind more trial and error but I do need some direction.

    Gruntre
     
    gruntre, Mar 12, 2005 IP
  2. dtan

    dtan Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why not use the 2 seperate queries and then compare in php? Depending on how you build your result arrays. . . check out http://jp2.php.net/manual/en/function.array-diff.php and http://jp2.php.net/manual/en/function.array-diff-assoc.php.

    So instead of
    
       echo($row["email"] . "<br>");
    
    PHP:
    store the results into an array.
    
    $subscribed_addresses[] = $row['email'];
    
    PHP:
    then do something similar for the second query. You should now have 2 arrays.
    
    $a = array_diff($subscribed_addresses, $confirmed_addresses);
    
    foreach( $a as $email ) {
    echo $email.'<br>';
    }
    
    PHP:
    Should do the trick. Of course none of this is actually tested. . .I will leave that up to you. ;)
     
    dtan, Mar 12, 2005 IP
  3. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can either use a sub-select:

    select col1, ... 
    from t1 
    where col1 not in (select col1 from t2 where ...)
    Code (markup):
    or an outer join:

    select col1, ... 
    from t1 left outer join t2 on t1.col1 = t2.col1 
    where t2.col1 is NULL and ...
    Code (markup):
    J.D.
     
    J.D., Mar 12, 2005 IP
  4. gruntre

    gruntre Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Tried this:[
    <?php 
    
    $subscribing_addresses = @mysql_query("SELECT email FROM orders WHERE edata = '$Yes subrscribe me'"); 
    $subscribed_addresses[] = $row["email"];
    
    mysql_select_db($DB2Name) or die("Unable to select database $DBName");
    $subscribed_addresses = @mysql_query ("SELECT email FROM phplist_user_user  WHERE confirmed = '1'"); 
    $subscribed_addresses[] = $row["email"]; 
    $a = array_diff($subscribing_addresses, $subscribed_addresses); 
    
    foreach( $a as $email ) { 
    echo $email.'<br>'; 
    }
    ?> 
    Code (markup):
    and it returns these errors:
     
    gruntre, Mar 14, 2005 IP
  5. gruntre

    gruntre Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks GD,

    I'm not sure how exactly to apply this to my code but I'll have a few goes at it and see if I can figure it. The help on a direction is appreciated.
    Gruntre
     
    gruntre, Mar 14, 2005 IP
  6. davedx

    davedx Peon

    Messages:
    429
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You removed the $row = mysql_fetch_array($result) that dtan's code relies on :D
     
    davedx, Mar 15, 2005 IP
  7. dtan

    dtan Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    heh. . .ya I was thinking you might do that. . . I'd tried to make it clearer by just including the echo line.

    Sorry, I'll try to be clearer in my examples next time.
     
    dtan, Mar 15, 2005 IP