Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    33

    Unanswered: PHP XML data to table

    Hello

    First of all, Sorry I'm PHP's noob.
    I'm completely new in PHP.

    I want to know how to retrieve XML and display the result in table in PHP.

    Supposed my XML data are stored in "X" table, "Y" row.
    And the XML data structure in "Y" row as follow.
    <XML id="">
    <A></A>
    <B></B>
    <C></C>
    </XML>

    I want to list out data from so that the result will be in table as follow.

    A B C
    data1 data1 data1
    data2 data2 data2

    How can I do that in php.
    Thanks

  2. #2
    Join Date
    Sep 2004
    Location
    Belgium
    Posts
    1,126
    Where do data1 and data2 come from?
    What does this have to do with DB2?
    --_Peter Vanroose,
    __IBM Certified Database Administrator, DB2 9 for z/OS
    __IBM Certified Application Developer
    __ABIS Training and Consulting
    __http://www.abis.be/

  3. #3
    Join Date
    Oct 2010
    Posts
    33
    I'm sorry for the lack information.

    I'm using DB2 Express-C on debian server and developing PHP application.

    The X table has 2 rows. Z and Y rows. The Z row stores id while the Y row stores XML data in native (using DB2 purexml function).

    I want to retrieve all the XML data from the Y row and represent it in table.

    The XML data structure in "Y" row as follow.
    For example.
    <XML id="1">
    <A>data1</A>
    <B>data2</B>
    <C>data3</C>
    </XML>

    <XML id="2">
    <A>data4</A>
    <B>data5</B>
    <C>data6</C>
    </XML>

    .
    .
    .

    <XML id="n">
    <A>datax</A>
    <B>datay</B>
    <C>dataz</C>
    </XML>



    I want to list out data so that the result will be in table as follow.

    A B C
    data1 data2 data3
    data4 data5 data6
    .
    .
    .
    datax datay dataz

    How can I do that in php.
    Thanks

  4. #4
    Join Date
    Sep 2004
    Location
    Belgium
    Posts
    1,126
    Not 100% sure if I understood your database design correctly, but it could be something in the following style:
    Code:
    <html><body><table><th><td>A</td><td>B</td><td>C</td></th>
    <?php
     $database = 'SAMPLE'; $user = 'db2inst1'; $password = 'ibmdb2';
     $conn = db2_connect($database, $user, $password);
     $sql =
     'SELECT  XMLQUERY(\'$Y/A\' PASSING Y) AS a,
              XMLQUERY(\'$Y/B\' PASSING Y) AS b,
              XMLQUERY(\'$Y/C\' PASSING Y) AS c
      FROM x';
    $stmt = db2_prepare($conn, $sql);
    $result = db2_execute($stmt);
    while ($row = db2_fetch_array($stmt)) {
        print "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>";
    } ?>
    </table></body></html>
    For the DB2 bindings in PHP, and how to install, see PHP: IBM DB2 Functions - Manual
    Last edited by Peter.Vanroose; 01-07-12 at 18:16.
    --_Peter Vanroose,
    __IBM Certified Database Administrator, DB2 9 for z/OS
    __IBM Certified Application Developer
    __ABIS Training and Consulting
    __http://www.abis.be/

  5. #5
    Join Date
    Oct 2010
    Posts
    33
    Thanks for the reply.

    However I got this errors.
    Warning: db2_execute(): Statement Execute Failed in XXX.php on line 17 Warning: db2_fetch_array(): Column information cannot be retrieved in XXX.php on line 18

    The errors occurred on
    (line 17) $result = db2_execute($stmt);
    (line 18) while ($row = db2_fetch_array($stmt)) {

    I guess there is something wrong on sql statement.

  6. #6
    Join Date
    Sep 2004
    Location
    Belgium
    Posts
    1,126
    Quote Originally Posted by tempe View Post
    I guess there is something wrong on sql statement.
    In that case, first try the SQL statement (literally, as it would be passed by PHP) in a "normal" DB2 interface, and modify it until it works. There could, e.g., be a problem with the table or column name(s). In my example the table is called "X" and the column is called "Y"; you'll possibly have to replace those.
    --_Peter Vanroose,
    __IBM Certified Database Administrator, DB2 9 for z/OS
    __IBM Certified Application Developer
    __ABIS Training and Consulting
    __http://www.abis.be/

Posting Permissions

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