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

    Unanswered: DB2 with PHP. How to fetch last row only?

    Hello

    I have a table with n columns

    I want to fetch data from last column only.
    How can I do that without have to define the column name or column max number.

    As example

    <?php
    session_start();
    $database = '123';
    $user = ($_SESSION['id']);
    $password = ($_SESSION['password']);
    $hostname = '456';
    $port = 50000;

    $conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;" .
    "HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID= $user;PWD=$password;";

    $conn = db2_connect($conn_string, '', '');

    if ($conn) {
    $sql = 'SELECT * FROM xxx ';
    $stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));

    if (!$stmt) {
    echo 'SQLSTATE value: ' . db2_stmt_error();
    echo 'with Message: ' . db2_stmt_errormsg();
    }

    while ($row = db2_fetch_array($stmt)) {
    echo "\t<tr><td>$row[3]</td><td>$row[2]</td><td>$row[1]</tr>\n";
    }
    db2_close($conn);
    }
    else {
    echo "Connection failed.";
    }
    ?>
    I want to echo "\t<tr><td>$row[n]</td></tr>\n";

    Is that posible?
    Thank You

  2. #2
    Join Date
    May 2003
    Location
    USA
    Posts
    5,737
    I am not exactly sure what you trying to do, but you can find the list of column names for a table in SYSCAT.COLUMNS catalog view. The Catalog Tables are documented in the appendix of the SQL Reference Vol 1.
    M. A. Feldman
    IBM Certified DBA on DB2 for Linux, UNIX, and Windows
    IBM Certified DBA on DB2 for z/OS and OS/390

  3. #3
    Join Date
    Oct 2010
    Posts
    33
    Thanks for the advice...

    I know the column name and number of columns for each table.

    However, I want retrieve data (from last column) from many tables that is the number of column for each table is different.

    So I don't have to consider the table name.

    Is there any other method to echo "\t<tr><td>$row[last column]</td></tr>\n";

  4. #4
    Join Date
    Oct 2010
    Posts
    33
    SOLVED

    $stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));
    $columns = db2_num_fields($stmt);
    $col = ($columns-1);

    -----
    echo "\t<tr><td>$row[$col]</td></tr>\n";

Posting Permissions

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