odbc_field_name

(PHP 3 >= 3.0.6, PHP 4, PHP 5)

odbc_field_name -- Get the columnname

Description

string odbc_field_name ( resource result_id, int field_number )

odbc_field_name() will return the name of the field occupying the given column number in the given ODBC result identifier. Field numbering starts at 1. FALSE is returned on error.


add a note add a note User Contributed Notes
jezzghost
22-Feb-2006 02:32
Note that there is a known limitation with this which truncates the length of the returned field name to 31 characters without warning.
hayes029 at bama dot ua dot edu
14-Apr-2004 01:01
In search of a function that would simply return an array with the names of the fields in a result identifier, the only thing I could find was the odbc_field_name function.  So, for anyone else looking for such a function, here's the (very simple) function I wrote:

function odbc_field_names ($result) {
  for ($i=1; $i <= odbc_num_fields($result); $i++) $return_array[$i-1] = odbc_field_name($result, $i);
  return $return_array;
}

Very simple, I know, but I thought it might be helpful.
andrea dot galli at acotel dot com
29-Apr-2003 11:12
Example: function field name.

$Link_ID = odbc_connect("DSN", "user", "pass");

$query = "SELECT * FROM products";

$Query_ID = odbc_exec($Link_ID, $query);

while($field = $field_name($Query_ID ))
{
     echo("Field: $field<br />\n");
}

---------------------

function field_name($PrQuery_ID)
{     
     if($Column < odbc_num_fields($PrQuery_ID))
     {     
         $Column += 1;
         $FieldName = odbc_field_name($PrQuery_ID, $Column);

         return $FieldName;
     }     
     else   
     {     
         return 0;
     }     
}
aleckzandr at yahoo dot com
17-Apr-2003 03:04
Well, I've been into PHP for four hours and thanks to "my predecessors before me" (gold163, curt, et al.) I've managed the following. The first thing I try to learn with any web scripting language is to build a dynamic table from a data source. (One thing you didn't have to do gold -previous post- is build an array for the field value.) Cheers! Alex

<html>
 <head>
  <title>PHP Database Example</title>
 </head>
 <style type="text/css">
 <!--
  body {font: 10pt/12pt Tahoma, Verdana, Helvetica, sans-serif; color: indigo; margin: .25in .5in }
  table {color:Navy; background-color:AntiqueWhite; border-color:Maroon; border-style:Solid; border-width: 2px; }
  th {color: blue; font-weight: bold; }
  td {font-size: smaller; }
  .mytable {color:Maroon; background-color:White; border-color:Navy; border-style:Solid; border-width: 1px; }
  th.mytable {background-color:#C0C0C0; }
 //-->
 </style>
 <body>

 <p><?php echo date("j F, Y"); ?></p>
<?php

$db
= odbc_connect("eSell22MDB","","");
$result = odbc_exec($db, "select ProductID, ProductName, Description1 from Products");

// cool function - returns table
odbc_result_all($result, "border=\"1\" class=\"def\"");

$result = odbc_exec($db, "select * from Products") or die("Select failed");

$myUtil = new Utilities();

$myUtil->standard_table($result,"mytable");

class
Utilities {

     function
standard_table($result,$class="")
     {
        
// To format your table if you want to use cascading style sheets
        
if ($class == "")
         {
            
$css_table = " border=\"1\"";
            
$css_tr = "";
            
$css_th = "";
            
$css_td = "";
         }
         else
         {
            
$css_table = " class=\"$class\"";
            
$css_tr = " class=\"$class\"";
            
$css_th = " class=\"$class\"";
            
$css_td = " class=\"$class\"";
         }

        
// Create field names for table header row
        
$i = 0;
        
$fieldCount = odbc_num_fields($result);
         echo
"  <table$css_table>\n";
         echo
"  <tr$css_tr>\n";

         while (
$i < $fieldCount)
         {
            
$i++;
            
$fieldName = odbc_field_name($result, $i);
             echo
"    <th$css_th>$fieldName</th>\n";
         }
         echo
"  </tr>\n";

        
# Create table data rows for query result
        
while (odbc_fetch_row($result))
         {
            
$i = 0;
             echo
"  <tr$css_tr>\n";
             while (
$i < $fieldCount)
             {
                
$i++;
                
$fieldData = trim(odbc_result($result, $i));
                 if (
$fieldData  == "")
                     echo
"    <td$css_td>&nbsp;</td>\n";
                 else
                     echo
"    <td$css_td>$fieldData</td>\n";
             }
             echo
"  </tr>\n";
         }
         echo
"  </table>";
     }
}
// class Utilities

?>

 </body>
</html>
gold163 at lisco dot com
26-Mar-2003 01:10
Using your code, and taking it a step further, I can create a standard table from a single line of code by calling a function from my include file - the bonus is, that I can optionally provide a parameter for the name of my style sheet class - further simplifying my formatting of the table.

What synergy you find in these forums - eh?

$Conn = odbc_connect('dsn','user','pass');
$query = "SELECT * FROM yourtable";
$result = odbc_exec($Conn, $query) or die('Select failed!');

standard_table($result);

Function standard_table($result,$class='')
{
# To format your table if you want to use cascading style sheets
 if ($class == '')
 {
  $css_table = ' border=1';
  $css_tr = '';
  $css_th = '';
  $css_td = '';
 }
 else
 {
  $css_table = ' class=\"$class\"';
  $css_tr = ' class=\"$class\"';
  $css_th = ' class=\"$class\"';
  $css_td = ' class=\"$class\"';
 }

# Create field names for table header row
$i = 0;
$fCount = odbc_num_fields($result);
echo "<table$css_table><tr>";
  while ($i < $fCount)
  {
   $i++;
   $fName = odbc_field_name($result, $i);
   echo "<th>$fName</th>";
  }
echo "</tr>";

# Create table data rows for query result
$i = 0;
$fCount = odbc_num_fields($result);
while (odbc_fetch_row($result))
{
echo "<tr>";
  while ($i < $fCount)
  {
   $i++;
   $fName = odbc_field_name($result, $i);
   $job[$fName] = odbc_result($result, $i);
   echo "<td>$job[$fName]</td>";
  }
echo "</tr>";
$i = 0;
}
echo "</table>";
}
curt at digmo dot com
01-Aug-2001 09:58
I turned Jason's code into a function to roughly mimic the mysql_fetch_array function. I'm not a programmer and I've been messing with PHP less than a week, so I imagine there's a more efficient method that what I've come up with.


function odbc_fetch_array($rownum, $res)
{

$i = 0;
$fCount = odbc_num_fields($res);
odbc_fetch_row($res, $rownum);
   while ($i < $fCount)
       {
   $i++;
   $fName = odbc_field_name($res, $i);
   $myrow[$fName] = odbc_result($res, $i);
         }
$i=0;
return $myrow;
}
11-Mar-2001 08:11
Wow, I finally have something to contribute.  :><br><br>
If you, like me, have been seeking a way to name and fill your variables with the appropriate names an values, rather than naming every variable and using odbc_result($result, 1), odbc_result($result, 2), etc...Then this little loop is for you!  It would probably be nice to use as function, but I'm sure you can do that on your own, eh?<br><br>

<?
$query
= "SELECT * FROM TableName";
$result = odbc_exec($conn, $query) or die('Select failed!');
$i = 0;
$fCount = odbc_num_fields($result);

while (
odbc_fetch_row($result))
{
while (
$i < $fCount)
{
$i++;
$fName = odbc_field_name($result, $i);
$job[$fName] = odbc_result($result, $i);
 }
$i=0;
}
?>



This should be pretty simple code to follow, you can address your variables at any time later using the column names from your table.  For now I am addressing them with their real values and using this simply to avoid having to type out all the variable names in the top of my code.  Have fun.

Jason/ArtHacker.com