Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2007
    Posts
    6

    Unanswered: 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:

    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/

  2. #2
    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

  3. #3
    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/

  4. #4
    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/

  5. #5
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    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

Posting Permissions

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