odbc_tables

(PHP 4, PHP 5, PHP 7, PHP 8)

odbc_tablesGet the list of table names stored in a specific data source

说明

odbc_tables ( resource $odbc , string|null $catalog = null , string|null $schema = null , string|null $table = null , string|null $types = null ) : resource|false

Lists all tables in the requested range.

To support enumeration of qualifiers, owners, and table types, the following special semantics for the catalog, schema, table, and table_type are available:

  • If catalog is a single percent character (%) and schema and table are empty strings, then the result set contains a list of valid qualifiers for the data source. (All columns except the TABLE_QUALIFIER column contain NULLs.)
  • If schema is a single percent character (%) and catalog and table are empty strings, then the result set contains a list of valid owners for the data source. (All columns except the TABLE_OWNER column contain NULLs.)
  • If table_type is a single percent character (%) and catalog, schema and table are empty strings, then the result set contains a list of valid table types for the data source. (All columns except the TABLE_TYPE column contain NULLs.)

参数

odbc

ODBC 连接标识符,详见 odbc_connect()

catalog

The catalog ('qualifier' in ODBC 2 parlance).

schema

The schema ('owner' in ODBC 2 parlance). 此参数接受下列查询模式: % 来匹配零到多个字符, _ 来匹配单个字符。

table

The name. 此参数接受下列查询模式: % 来匹配零到多个字符, _ 来匹配单个字符。

types

If table_type is not an empty string, it must contain a list of comma-separated values for the types of interest; each value may be enclosed in single quotes (') or unquoted. For example, 'TABLE','VIEW' or TABLE, VIEW. If the data source does not support a specified table type, odbc_tables() does not return any results for that type.

返回值

Returns an ODBC result identifier containing the information 或者在失败时返回 false.

The result set has the following columns:

  • TABLE_CAT
  • TABLE_SCHEM
  • TABLE_NAME
  • TABLE_TYPE
  • REMARKS
Drivers can report additional columns.

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

更新日志

版本 说明
8.0.0 schema, table and types are now nullable.

范例

Example #1 List Tables in a Catalog

<?php
$conn 
odbc_connect($dsn$user$pass);
$tables odbc_tables($conn'SalesOrders''dbo''%''TABLE');
while ((
$row odbc_fetch_array($tables))) {
    
print_r($row);
    break; 
// further rows omitted for brevity
}
?>

以上例程的输出类似于:

Array
(
    [TABLE_CAT] => SalesOrders
    [TABLE_SCHEM] => dbo
    [TABLE_NAME] => Orders
    [TABLE_TYPE] => TABLE
    [REMARKS] =>
)

参见

User Contributed Notes

narcomweb at wanadoo dot fr 05-Nov-2005 09:33
Here a Code for listing Table names
<?php
$dbh
= odbc_connect($dsn, $user, $pwd);

  
$result = odbc_tables($dbh);

  
$tables = array();
   while (
odbc_fetch_row($result)){
     if(
odbc_result($result,"TABLE_TYPE")=="TABLE")
       echo
"<br>".odbc_result($result,"TABLE_NAME");

   }
?>
You don't have views or System tables with.
Only simple tables in your database.
pmains at ux dot com 22-Jul-2005 07:39
This probably works for a variety of formats, but if you want a list of *just* the table names from an SQL dsn source (I'm using MSSQL), here's the code.

<?php

# ... assign $dsn, $uid, & $pwd ...

$dbh = odbc_connect($dsn, $uid, $pwd);

   
$result = odbc_tables($dbh);
   
   
$tables = array();
    while (
odbc_fetch_row($result))
       
array_push($tables, odbc_result($result, "TABLE_NAME") );
   
    foreach(
$tables as $t ) {
        echo
"$t\n";   
    }

?>
iggvopvantoodlwin 01-Mar-2004 07:42
With regard to the note made on results not working.
Test the database with the easy:

odbc_result_all(odbc_tables($db));

$db is obviously a connected batadase. Then start to experiment:

if(!$odbcr=odbc_tables($db,"udb","", "%", "'TABLE'"))

"udb" is the DNS - aka 'name of my ODBC database in the Windows ODBC thingamy'. In result_all the full path was shown but I just used the name I assigned; either should work.

The second parameter "" is listed by result_all as "TABLE_SCHEM" and all items were "NULL", so I have put "".

The third parameter is "%". According to result_all this col is "TABLE_NAME", so I could have put the name of one of my tables, i.e. "Address".

In my case I have an Access database setup with several tables. In ODBC I have created a link. Running the all on everything result above shows a set of system tables which I do not need to know about at this point so I look at the result and then build my new table check using the "TABLE" string as the tables I am interested in are listed as "TABLE" under their "TABLE_TYPE" column.
liquidicee at hotmail dot com 10-Mar-2001 08:02
Here's how to get a list of all the tables in your database.. with an actual example of how its done and how to get the results.. and you don't need to put in schema and all that other crap

<?php
$conn
= odbc_connect("$database", "$username", "$password");
$tablelist = odbc_tables($conn);
while (
odbc_fetch_row($tablelist)) {
if (
odbc_result($tablelist, 4) == "TABLE")
echo
odbc_result($tablelist, 3) ."<br>";
}
?>

to understand what the above is doing,
use odbc_result_all($tablelist); this will show you EVERYTHING returned by odbc_tables() then you can look through it and see better how odbc_tables() works and what exactly it returns in the string to get a better idea on how to deal with it.
it would have saved me alot of time if i would have just taken a look at the full string returned by odbc_tables(), so i suggest you take the minute or two and look... here is an example of how to do it..which would have been helpful for me ;x.

<?php
$conn
= odbc_connect("$database", "$username", "$password");
$tablelist = odbc_tables($conn);
while (
odbc_fetch_row($tablelist)) {
echo
odbc_result_all($tablelist);
}
?>

hopefully this will help some people.. i have alot more to add about this but no time :(
so again hope this helps.
Liquidice
PHP8中文手册 站长在线 整理 版权归PHP文档组所有