Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : ODBC Functions (Unified) : odbc_field_name

odbc_field_name

Get the columnname (PHP 4, PHP 5)
string odbc_field_name ( resource result_id, int field_number )

Examples ( Source code ) » odbc_field_name

<?php
    
//connect to database
    
$Link odbc_connect("mysql-nav""phpUser""");

    
//switch to freetrade database
    
odbc_do($Link"USE ft3");

    
// execute query
    
$Query "SELECT * " .
    
$Query .= "FROM MyTable ";
    
$Result odbc_do($Link$Query);
    print(
"<table border=\"1\">\n");
    print(
"<tr>\n");
    print(
"<th>Number</th>");
    print(
"<th>Name</th>");
    print(
"<th>Type</th>");
    print(
"<th>Length</th>");
    print(
"<th>Precision</th>");
    print(
"<th>Scale</th>");
    print(
"</tr>\n");

    
$cols odbc_num_fields($Result);
    for(
$c=1$c <= $cols$c++)
    {
        print(
"<tr>\n");
        print(
"<td>$c</td>");
        print(
"<td>".odbc_field_name($Result$c)."</td>");
        print(
"<td>".odbc_field_type($Result$c)."</td>");
        print(
"<td>".odbc_field_len($Result$c)."</td>");
        print(
"<td>".odbc_field_precision($Result$c)."</td>");
        print(
"<td>".odbc_field_scale($Result$c)."</td>");
        print(
"</tr>\n");
    }
    print(
"</table>\n");

    
//close connection
    
odbc_close($Link);
?>

Code Examples / Notes » odbc_field_name

11-mar-2001 12:11

Wow, I finally have something to contribute.  :>
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?
<?
$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


aleckzandr

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>


<?php echo date("j F, Y"); ?>
<?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

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>";
}


jezzghost

Note that there is a known limitation with this which truncates the length of the returned field name to 31 characters without warning.

hayes029

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.


curt

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;
}


andrea dot galli

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;
    }      
}


Change Language


Follow Navioo On Twitter
odbc_autocommit
odbc_binmode
odbc_close_all
odbc_close
odbc_columnprivileges
odbc_columns
odbc_commit
odbc_connect
odbc_cursor
odbc_data_source
odbc_do
odbc_error
odbc_errormsg
odbc_exec
odbc_execute
odbc_fetch_array
odbc_fetch_into
odbc_fetch_object
odbc_fetch_row
odbc_field_len
odbc_field_name
odbc_field_num
odbc_field_precision
odbc_field_scale
odbc_field_type
odbc_foreignkeys
odbc_free_result
odbc_gettypeinfo
odbc_longreadlen
odbc_next_result
odbc_num_fields
odbc_num_rows
odbc_pconnect
odbc_prepare
odbc_primarykeys
odbc_procedurecolumns
odbc_procedures
odbc_result_all
odbc_result
odbc_rollback
odbc_setoption
odbc_specialcolumns
odbc_statistics
odbc_tableprivileges
odbc_tables
eXTReMe Tracker