Page 1 of 1

Need help to return 2 query into a single table

Posted: Wed Jul 11, 2012 10:26 pm
by tempex
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

Code: Select all

 <?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";
    }

    ?>

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

Code: Select all

    Query 1 result
    1
    2
    3

    Query 2 result
    a
    b
    c
Instead of above, I want my result to look like this

Code: Select all


    Query 1 Result   Query 2 Result
    1                         a
    2                         b
    3                         c

How can I combine the result into single table in different column.

Thanks in advanced.

Re: Need help to return 2 query into a single table

Posted: Wed Jul 18, 2012 4:11 pm
by avibitton
i have nice solution for it , but if you didnt understand it compltely at least it will give you an idea how to solve it like you need .
i think you should use 2d array, for doing rows and columns ,and for the sec query use the modulo for echo just on pair number (2,4,6,...) it will look like something like that .
<td1 query1> <td2 query 2>
first you need to count how much object you have at the query and put it in $count than from query 2 $count2
for echo just in td2 i mean each second "td" use: if ($count2 % 2==0){ echo <td>things from query 2 </td>} else or else if {$count%2==1) means (1,3,5,7...) so it will echo the first td from query 1 , than from query 2 , than you need to do new row.

to do 2s array you can google it , or i guess you now how to do it .

good luck Avi .