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.

Need help to return 2 queryfrom 2 database into a single table in different column

Discussion in 'PHP' started by tempex, Jul 11, 2012.

  1. #1
    Hello,
    Need help to write php code which can return result of 2 query into a single table in different column
    Here is my code

    <?php
    ...
    
    $conn1 = db2_connect($database1, $user, $password);
    $conn2 = db2_connect($database2, $user, $password);
    if ($conn1) {
     if ($conn2) {
    $sql1 = $_POST["keyword1"];
    $stmt1 = db2_exec($conn1, $sql1, array('cursor' => DB2_SCROLLABLE));
    $columns1 = db2_num_fields($stmt1);
    $col1 = ($columns1-1);
    $name1 = db2_field_name($stmt1, $col1);
    print("<table border = '1'>\n");
    while ($row1 = db2_fetch_array($stmt1)) {
    print "\t<tr><td>$row1[$col1]</td></tr>\n";
    }
    print("</table>\n");
        db2_close($conn1);
    } 
    
    $sql2 = $_POST["keyword2"];
    $stmt2 = db2_exec($conn2, $sql2, array('cursor' => DB2_SCROLLABLE));
    $columns2 = db2_num_fields($stmt2);
    $col2 = ($columns2-1);
    $name2 = db2_field_name($stmt2, $col2);
    print("<table border = '1'>\n");
    while ($row2 = db2_fetch_array($stmt2)) {
    print "\t<tr><td>$row2[$col2]</td></tr>\n";
    }
    print("</table>\n");
    db2_close($conn2)
    ;}
    else {
        echo "Fail to connect to both databases";
    }
    
    ?>
    Code (markup):

    From code above, I tried to query from 2 different database and the result are return in separate table.

    
    [TABLE="class: grid, width: 150"]
    [TR]
    [TD="align: center"]Query 1 result[/TD]
    [/TR]
    [TR]
    [TD]1[/TD]
    [/TR]
    [TR]
    [TD]2[/TD]
    [/TR]
    [TR]
    [TD]3[/TD]
    [/TR]
    [/TABLE]
    
    [TABLE="class: grid, width: 150"]
    [TR]
    [TD="align: center"]Query 2 result[/TD]
    [/TR]
    [TR]
    [TD]a[/TD]
    [/TR]
    [TR]
    [TD]b[/TD]
    [/TR]
    [TR]
    [TD]c[/TD]
    [/TR]
    [/TABLE]
    
    Instead of above, I want my result to look like this
    
    [TABLE="class: grid, width: 300"]
    [TR]
    [TD="align: center"]Query 1 Result[/TD]
    [TD="align: center"]Query 2 Result[/TD]
    [/TR]
    [TR]
    [TD]1[/TD]
    [TD]a[/TD]
    [/TR]
    [TR]
    [TD]2[/TD]
    [TD]b[/TD]
    [/TR]
    [TR]
    [TD]3[/TD]
    [TD]c[/TD]
    [/TR]
    [/TABLE]
    
    
    
    
    Code (markup):
    How can I combine the result into single table in different column.

    Thanks in advanced.
     
    tempex, Jul 11, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    We'd need to know the database structure (table names, field names and the relationships between the fields in one table and the fields in the other table).

    If the tables are in the same database, you'd probably use a JOIN statement. If they're in 2 databases you'd get the data from each one with a separate query, then combine the data using PHP, and write it to the page.
     
    Rukbat, Jul 13, 2012 IP
  3. tempex

    tempex Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm sorry for the lack information.

    The target databases are 2 different target databases, but the target table structure is the same for both.


    • The table name is 'term'
    • The field name is 'term' (xml format)
    • The instance name is 'db2inst1'
    So the SQL query is
     select term from db2inst1.term
    Code (markup):
    for both table.

    Actually, there is no any relationship between the fields in one table and the fields in the other table.
    There only share same structure, and the number of data stored in both table is same.
    The returned result have completely same number of row.

    So, if I rewrite the previous code, it will be as below
    
    <?php ...  $conn1 = db2_connect($database1, $user, $password);
     $conn2 = db2_connect($database2, $user, $password); 
    if ($conn1) {  
    if ($conn2) { 
    
    $sql1 = 'select term from db2inst1.term'; 
    $stmt1 = db2_exec($conn1, $sql1, array('cursor' => DB2_SCROLLABLE)); 
    $columns1 = db2_num_fields($stmt1); $col1 = ($columns1-1);
     $name1 = db2_field_name($stmt1, $col1); 
    print("<table border = '1'>\n"); 
    while ($row1 = db2_fetch_array($stmt1)) { 
    print "\t<tr><td>$row1[$col1]</td></tr>\n";
     } print("</table>\n");
         db2_close($conn1); }  
    
     $sql2 = 'select term from db2inst1.term'; 
    $stmt2 = db2_exec($conn2, $sql2, array('cursor' => DB2_SCROLLABLE)); 
    $columns2 = db2_num_fields($stmt2); 
    $col2 = ($columns2-1); 
    $name2 = db2_field_name($stmt2, $col2); 
    print("<table border = '1'>\n"); 
    while ($row2 = db2_fetch_array($stmt2)) { 
    print "\t<tr><td>$row2[$col2]</td></tr>\n"; }
     print("</table>\n"); 
    db2_close($conn2) ;
    } else {   
      echo "Fail to connect to both databases"; }  
    ?>
    Code (markup):
     
    tempex, Jul 14, 2012 IP
  4. superlinksworld

    superlinksworld Member

    Messages:
    127
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    43
    #4
    Hi ,

    Why use two table

    here is solution
    
    
    <?php
    ...
    
    $conn1 = db2_connect($database1, $user, $password);
    $conn2 = db2_connect($database2, $user, $password);
    if ($conn1) {
     if ($conn2) {
    $sql1 = $_POST["keyword1"];
    $stmt1 = db2_exec($conn1, $sql1, array('cursor' => DB2_SCROLLABLE));
    $columns1 = db2_num_fields($stmt1);
    $col1 = ($columns1-1);
    $name1 = db2_field_name($stmt1, $col1);
    print("<table border = '1'>\n");
    print("<tr>>\n");
    while ($row1 = db2_fetch_array($stmt1)) {
    print "\t<td>$row1[$col1]</td>\n";
    }
    
        db2_close($conn1);
    } 
    
    $sql2 = $_POST["keyword2"];
    $stmt2 = db2_exec($conn2, $sql2, array('cursor' => DB2_SCROLLABLE));
    $columns2 = db2_num_fields($stmt2);
    $col2 = ($columns2-1);
    $name2 = db2_field_name($stmt2, $col2);
    
    while ($row2 = db2_fetch_array($stmt2)) {
    print "\t<td>$row2[$col2]</td>\n";
    }
    print("</tr></table>\n");
    db2_close($conn2)
    ;}
    else {
        echo "Fail to connect to both databases";
    }
    
    ?>
    
    PHP:
    Hope its helps

    Regards,

    D Najmi
     
    superlinksworld, Jul 14, 2012 IP
  5. tempex

    tempex Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thank for you tips..
    your suggested code return

    [TABLE="class: grid, width: 100"]
    [TR]
    [TD]1
    [/TD]
    [TD]2
    [/TD]
    [TD]3
    [/TD]
    [TD]a
    [/TD]
    [TD]b
    [/TD]
    [TD]c
    [/TD]
    [/TR]
    [/TABLE]


    I use two table because one for English and the other for Japanese data.
     
    tempex, Jul 14, 2012 IP
  6. superlinksworld

    superlinksworld Member

    Messages:
    127
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    43
    #6
    can you post CSS again please

    Regards,

    D Najmi
     
    superlinksworld, Jul 14, 2012 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    If you keep all the data in one table you'll make your life a lot easier. One SELECT will return all the data you want, in the form you want it.
     
    Rukbat, Jul 15, 2012 IP
  8. tempex

    tempex Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I'm sorry, I'm not using any CSS.
     
    tempex, Jul 15, 2012 IP
  9. tempex

    tempex Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ya, I know that. Because I'm not the one who store and manage the data. I just want to retrieve it.
     
    tempex, Jul 15, 2012 IP
  10. superlinksworld

    superlinksworld Member

    Messages:
    127
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    43
    #10
    ops
    Em sorry ..Ok here i edit that code again

    
    <?php
    ...
    
    $conn1 = db2_connect($database1, $user, $password);
    $conn2 = db2_connect($database2, $user, $password);
    if ($conn1) {
     if ($conn2) {
    $sql1 = $_POST["keyword1"];
    $stmt1 = db2_exec($conn1, $sql1, array('cursor' => DB2_SCROLLABLE));
    $columns1 = db2_num_fields($stmt1);
    $col1 = ($columns1-1);
    $name1 = db2_field_name($stmt1, $col1);
    print("<table border='1' cellspacing='5' cellpadding='10'>\n");
    print("<tr>\n");
    print("<td><p>Query 1</p>\n");
    print("<table border='1' cellspacing='5' cellpadding='9'>\n");
    while ($row1 = db2_fetch_array($stmt1)) {
    print "\t<tr><td>$row1[$col1]</td></tr>\n";
    }
    
        db2_close($conn1);
    } 
    print("</table>\n");
    print("</td>\n");
    print("<td><p>Query 2</p>\n");
    print("<table border='1' cellspacing='5' cellpadding='9'>\n");
    $sql2 = $_POST["keyword2"];
    $stmt2 = db2_exec($conn2, $sql2, array('cursor' => DB2_SCROLLABLE));
    $columns2 = db2_num_fields($stmt2);
    $col2 = ($columns2-1);
    $name2 = db2_field_name($stmt2, $col2);
    while ($row2 = db2_fetch_array($stmt2)) {
    print "\t<tr><td>$row2[$col2]</td></tr>\n";
    }
    print("</table>\n");
    print("</td>\n");
    print("</tr>\n");
    print("</table>\n");
    db2_close($conn2)
    ;}
    else {
        echo "Fail to connect to both databases";
    }
    
    ?>
    
    Code (markup):
    I hope its help

    You can work around cellpadding / cell spacing in case of large data in column to adjust that .

    Why dont u use <div> then <ul><li> as css instead?

    Regards,

    D Najmi
     
    Last edited: Jul 17, 2012
    superlinksworld, Jul 17, 2012 IP