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.
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.
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):
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
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.
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.
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