odbc_columns

(PHP 4, PHP 5, PHP 7)

odbc_columnsLists the column names in specified tables

Descrierea

odbc_columns ( resource $odbc , string|null $catalog = null , string|null $schema = null , string|null $table = null , string|null $column = null ) : resource|false

Lists all columns in the requested range.

Parametri

odbc

Identificatorul conexiunii ODBC, accesați odbc_connect() pentru detalii.

catalog

Catalogul ('calificativul' în jargonul ODBC 2).

schema

Schema ('proprietarul' în jargonul ODBC 2). Acest parametru acceptă următoarele șabloane de căutare: % pentru a se potrivi cu zero sau mai multe caractere și _ pentru a se potrivi cu un singur caracter.

table

The table name. Acest parametru acceptă următoarele șabloane de căutare: % pentru a se potrivi cu zero sau mai multe caractere și _ pentru a se potrivi cu un singur caracter.

column

The column name. Acest parametru acceptă următoarele șabloane de căutare: % pentru a se potrivi cu zero sau mai multe caractere și _ pentru a se potrivi cu un singur caracter.

Valorile întoarse

Returns an ODBC result identifier sau false în cazul eșecului.

The result set has the following columns:

  • TABLE_CAT
  • TABLE_SCHEM
  • TABLE_NAME
  • COLUMN_NAME
  • DATA_TYPE
  • TYPE_NAME
  • COLUMN_SIZE
  • BUFFER_LENGTH
  • DECIMAL_DIGITS
  • NUM_PREC_RADIX
  • NULLABLE
  • REMARKS
  • COLUMN_DEF
  • SQL_DATA_TYPE
  • SQL_DATETIME_SUB
  • CHAR_OCTET_LENGTH
  • ORDINAL_POSITION
  • IS_NULLABLE
Driver-ele pot raporta coloane suplimentare.

The result set is ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME and ORDINAL_POSITION.

Istoricul schimbărilor

Versiune Descriere
8.0.0 schema, table and column are now nullable.

Exemple

Example #1 List Columns of a Table

<?php
$conn 
odbc_connect($dsn$user$pass);
$columns odbc_columns($conn'TutorialDB''dbo''test''%');
while ((
$row odbc_fetch_array($columns))) {
    
print_r($row);
    break; 
// further rows omitted for brevity
}
?>

Exemplul de mai sus va afișa ceva similar cu:

Array
(
    [TABLE_CAT] => TutorialDB
    [TABLE_SCHEM] => dbo
    [TABLE_NAME] => TEST
    [COLUMN_NAME] => id
    [DATA_TYPE] => 4
    [TYPE_NAME] => int
    [COLUMN_SIZE] => 10
    [BUFFER_LENGTH] => 4
    [DECIMAL_DIGITS] => 0
    [NUM_PREC_RADIX] => 10
    [NULLABLE] => 0
    [REMARKS] =>
    [COLUMN_DEF] =>
    [SQL_DATA_TYPE] => 4
    [SQL_DATETIME_SUB] =>
    [CHAR_OCTET_LENGTH] =>
    [ORDINAL_POSITION] => 1
    [IS_NULLABLE] => NO
)

A se vedea și

add a note add a note

User Contributed Notes 13 notes

up
10
Thomas
15 years ago
[MS SQL Server 2005/2008, PHP 5]

Imagine you would need to access the column names of a specific table, for instance to display them as table headers for fields with missing information. While browsing the documentation I was kind of lost how to use odbc_columns() without the usage of odbc_result_all() which outputs EVERYTHING in a single HTML table.

Here is a way to stuff all output into an array and then access only one or more fields of the odbc_columns() output:

<?php
include('connect.inc'); // <== Put all your database connection parameters in here. (DSN, PWD, USR, mssql_connect, etc.; returns $connection)

$outval = odbc_columns($connection, "your DB name", "%", "your table name", "%");

$pages = array();
while (
odbc_fetch_into($outval, $pages)) {
        echo
$pages[3] . "<br />\n"; // presents all fields of the array $pages in a new line until the array pointer reaches the end of array data
   
}
?>

Now your array $pages will have the following contents:
([x] is the array index displayed here for better understanding)

[0] TABLE_CAT <== your DB name
[1] TABLE_SCHEM <== dbo, your table scheme
[2] TABLE_NAME <== your table name
[3] COLUMN_NAME <== your column names (selected all with "%" in odbc_columns() )
[4] DATA_TYPE <== -8
[5] TYPE_NAME <== nchar (corresponds to -8, 11 f.i. is datetime and so on)
[6] COLUMN_SIZE <== num. val.
[7] BUFFER_LENGTH <== num. val.
[8] DECIMAL_DIGITS <== num. val. or NULL
[9] NUM_PREC_RADIX <== num. val. or NULL
[10] NULLABLE <== num. val.
[11] REMARKS <== num. val. or NULL
[12] COLUMN_DEF <== num. val. or NULL
[13] SQL_DATA_TYPE <== num. val.
[14] SQL_DATETIME_SUB <== num. val. or NULL
[15] CHAR_OCTET_LENGTH <== num. val. or NULL
[16] ORDINAL_POSITION <== num. val.
[17] IS_NULLABLE <== YES/NO
[18] SS_DATA_TYPE <== num. val.

Now you can access each field recursivly by its key and output only the DESIRED fields instead of having ALL output from odbc_result_all().
Please note that the array key starts at zero (0) instead of one (1), so echo $pages[3] selects COLUMN_NAME from the above list.

I hope this helps...

Cheers
Thomas
up
1
ke3wh at comcast dot net
19 years ago
The complete script to get into from a DSN MS Access DB Table and display it is below.

function Error_Handler( $msg, $cnx ) {
    echo "$msg \n";
    odbc_close( $cnx);
    exit();
    }

$cnx = odbc_connect( 'DSN_NAME' , '', '' );//connect to MSAccess
    if (!$cnx) {
      Error_handler( "Error in odbc_connect" , $cnx );
    } 

    $res400= odbc_columns($cnx,"DSN_NAME","","TABLE");
    echo odbc_result_all($res400);
up
1
php dot lpatrick at spamgourmet dot com
16 years ago
Since I was just looking for table descriptions of an MS Access file I didn't know the table structure of, I wrote this (where $inputfile is the Access file name):

<?php
$conn
= odbc_connect("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".$inputfile;, "", "");

$tabs = odbc_tables($conn);
$tables = array();
while (
odbc_fetch_row($tabs)){
    if (
odbc_result($tabs,"TABLE_TYPE")=="TABLE") {
       
$table_name = odbc_result($tabs,"TABLE_NAME");
       
$tables["{$table_name}"] = array();
       
$cols = odbc_exec($conn,'select * from `'.$table_name.'` where 1=2');  // we don't want content
     
$ncols = odbc_num_fields($cols);
        for (
$n=1; $n<=$ncols; $n++) {
           
$field_name = odbc_field_name($cols, $n);
           
$tables["{$table_name}"]["{$field_name}"]['len'] = odbc_field_len($cols, $n);
           
$tables["{$table_name}"]["{$field_name}"]['type'] = odbc_field_type($cols, $n);
        }
    }
}
odbc_close ($conn);
print_r($tables);
?>
up
2
eion at robbmob dot com
12 years ago
Sometimes the array keys of the results of odbc_columns() can be in lower case, instead of upper case (eg 'column_name' instead of 'COLUMN_NAME').  This can vary depending on which ODBC driver you are connecting to.
up
1
jeremie dot legrand at komori-chambon dot fr
16 years ago
Here is the way to use odbc_columns() with "Attunity Connect", an ODBC connector to VMS :

$db = "my_database";
$table = "my_table";
$con = odbc_connect($db, "user", "password");
$result = odbc_columns($con, $db, "", $table, "%");
while (odbc_fetch_row($resu)) {
    echo odbc_result_all($resu);
}
up
1
LyleE at LocalMotion dot com
23 years ago
Took me a while to figure out this command, thought I would save some other people the time.  We couldn't quite figure out what the qualifier was.  For MSSQL 7.0, it is the Database that you are connecing to.  For the pubs database it would look something like this.

$rs = odbc_columns($DBConnection, "Pubs", "%", "jobs");

That would be for showing the jobs table.
up
0
tom at jargonsoft dot com
5 years ago
The docs on this page are among the worst in php. I know docs are traditionally terrible for open source code, but this is NOT acceptable:
Qualifer: the qualifer. REALLY??
How about: ComplexFieldWithManyRules: the ComplexFieldWithManyRules.
Give me a break.
How about actually defining the meaning and how to use it?
Along with several EXAMPLES!!
You are giving software developers a bad name with junk like this.
up
0
Artur
17 years ago
This is the only way I could actually get field names using odbc_columns. Hope it will be usefull for someone.

$result = odbc_columns($odbc,$dbhost,"dbo", "KIR_ViolationDetail");

while (odbc_fetch_row($result))
{
  echo odbc_result($result,"COLUMN_NAME");
}
up
0
laundro at gmail dot com
20 years ago
Getting all column names from Excel with ODBC:
$cols = odbc_columns($connection, $filename, NULL, $sheet);

where:
$connection is the result of your odbc_connect;
$filename is the filename of the Excel file;
$sheet is the name of the Excel worksheet.

This is useful when you want to query an Excel file without having to name ranges beforehand. With the results obtained from the above command, you can populate an array and use its contents (ie the column names) for further querying.
up
0
netaminas at hotamil dot com
20 years ago
Connect with IBM Client Access 32-bit ODBC driver

To access table information in DB2 iSeries (AS/400) via ODBC Driver i've tried with this code and worked!!!

$conn_ODBC = odbc_connect("DSN", "USER", "PASSW") or die;
$tabela = "table name in IBM iSeries";
$libname = "library name in IBM iSeries"

$res400 = odbc_columns($conn_ODBC, "DSN", $libname, $tabela, "%")  or die("<p><font color=#FF0000>Erro Na Leitura da Tabela ".$tabela." do AS/400: ".odbc_errormsg());

echo odbc_result_all($res400);

Netaminas.com
PORTUGAL
up
0
Anonymous
21 years ago
ODBC & MS ACCESS :

odbc_columns($conn,"DSN_NAME","","TABLE_NAME");
up
0
Sergio Sartori
21 years ago
Using this function on a MS SQL Server 2000 database connection with the syntax:

$res = odbc_columns($connId, $dbName, "%", $tableName, "%");

I actually get a result set with THESE columns name:

TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, DATA_TYPE, TYPE_NAME, COLUMN_SIZE, BUFFER_LENGTH, DECIMAL_DIGITS, NUM_PREC_RADIX, NULLABLE, REMARKS

in the order displayed.
up
0
josh at engledental dot com
22 years ago
(PHP4.2.1, Win2k, MSSQL 2K)

The result id returned from this function does not appear to behave exactly the same as a result id returned from a query. 

I get all sorts of errors when I try to use subsequent odbc functions on the result id, such as:

odbc_fetch_into - This function only seems to work if I don't specify a row number or if the row number = 0.

odbc_fetch_row - This function will not return a row from the result set.  Under some scenarios, I am able to use it in a while loop, but its seems buggy.  Hence, my odbc_num_of_rows function (hack to replace the non-functional odbc_num_rows function) also doesn't work.

I recommend dumping the results into an array immediately and freeing the result id created by this function.

ps - If anyone has any further insight or workarounds, I would appreciate them.
To Top