If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > DB2 with PHP. How to fetch last row only?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-18-12, 23:17
tempe tempe is offline
Registered User
 
Join Date: Oct 2010
Posts: 27
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

Quote:
<?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
Reply With Quote
  #2 (permalink)  
Old 01-18-12, 23:38
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
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
Reply With Quote
  #3 (permalink)  
Old 01-18-12, 23:48
tempe tempe is offline
Registered User
 
Join Date: Oct 2010
Posts: 27
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";
Reply With Quote
  #4 (permalink)  
Old 01-19-12, 01:23
tempe tempe is offline
Registered User
 
Join Date: Oct 2010
Posts: 27
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";
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On