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_columns difficulty

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-17-07, 08:11
SimonJ621 SimonJ621 is offline
Registered User
 
Join Date: Aug 2007
Posts: 6
db2_columns difficulty

Hi,

I am attempting to retrieve column names from a table in DB2 using php. While I have a valid connection to the database, I cannot get the db2_columns function to return results. I'm very new to using DB2 and would appreciate any help or suggestions. The following is the code I am attempting:

$con = db2_pconnect($dbname, $username, $password);
PHP Code:
if ($con) {
   
   
$schema "%";
   
$tbl "table1";
   
   
$store db2_columns($con""$schema$tbl"");

   echo 
db2_result($store"COLUMN_NAME"); 
If I var_dump $store, I get the following:

Quote:
resource(3) of type (DB2 Statement) bool(false)
I know the connection works because I have been able to run sql and return results using the same connection. Thank you for all your help.

Jason
---------------------------------------------------------
http://www.elefoo.com/
http://www.blackspyraldancer.com/
Reply With Quote
  #2 (permalink)  
Old 08-17-07, 08:52
guyprzytula guyprzytula is offline
Registered User
 
Join Date: Jun 2006
Posts: 471
is the tablename in lowercase. this might be case sensitive
__________________
Best Regards, Guy Przytula
DB2 UDB LUW certified V6/7/8
Reply With Quote
  #3 (permalink)  
Old 08-17-07, 09:05
SimonJ621 SimonJ621 is offline
Registered User
 
Join Date: Aug 2007
Posts: 6
I actually didn't write in the actual table name for the above code snippet. The actual table name is in all caps and works using Select statements. But I could see myself making that mistake :/

Jason
-----------------------
http://www.elefoo.com/
http://www.blackspyraldancer.com/
Reply With Quote
  #4 (permalink)  
Old 08-17-07, 10:35
SimonJ621 SimonJ621 is offline
Registered User
 
Join Date: Aug 2007
Posts: 6
Well, I didn't figure out how to use db2_columns correctly, but I'm under a time constraint so the following is my work-around to grab the number of columns and the column names:

PHP Code:
$dbconn db2_pconnect($dbname$username$password);

if (
$dbconn) {
   
$sql "SELECT * FROM TABLE1 FETCH FIRST 1 ROWS ONLY";
   
$stmt db2_prepare($dbconn$sql);
   
$db2_execute($stmt);

   
$colNumber 0;

   while (
$result db2_fetch_array($stmt)) {
      
$colNumber count($result);
   }

   
$i 0;
   
$colNames = array();

   while (
$i $colNumber) {
      
$colNames[$i] = db2_field_name($stmt$i);
      
$i++;
   }
else {
   echo 
"Connection failed.<br />";
   echo 
db2_conn_errormsg();


Jason
-------------------------
http://www.elefoo.com/
http://www.blackspyraldancer.com/
Reply With Quote
  #5 (permalink)  
Old 08-17-07, 13:36
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I don't know how "db2_columns" works (and I was to lazy to look it up), but you could also use the SYSCAT.COLUMNS catalog view to get the information about the columns in a table.

p.s: Using "SELECT *" is generally a bad idea.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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