CakeFest 2024: The Official CakePHP Conference

odbc_tables

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

odbc_tables指定したデータソースに保存されたテーブルの名前のリストを取得する

説明

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

指定した範囲の全てのテーブルリストを得ます。

限定子、所有者、テーブル名を数えるために、 catalogschematabletable_type について以下のような特別な記号が使用可能です。

  • catalog がパーセント記号 (%) のみで、 schema および table が空文字列であった場合、結果にはそのデータソースに関する 有効な限定子のリスト (null を有する TABLE_QUALIFIER カラム以外の 全てのカラム)が含まれます。
  • schema がパーセント記号 (%) のみで、 catalog および table が空文字列の場合、結果にはその データソースに関する有効な所有者のリスト (null を有する TABLE_OWNER カラム以外の全てのカラム) が含まれます。
  • table_type がパーセント記号 (%) のみで、 catalogschematable が空文字列の場合、結果にはそのデータソースに 関する有効なテーブル型のリスト (null を有する TABLE_TYPE カラム以外の全てのカラム) が含まれます。

パラメータ

odbc

ODBC 接続 ID。詳細は odbc_connect() を参照ください。

catalog

カタログ(ODBC 2 の用語では '修飾子')。

schema

スキーマ (ODBC 2 の用語では '所有者')。 このパラメータには以下の検索パターンを使用できます。 % はゼロ個以上の文字にマッチし、 _ はひとつの文字にマッチします。

table

名前。 このパラメータには以下の検索パターンを使用できます。 % はゼロ個以上の文字にマッチし、 _ はひとつの文字にマッチします。

types

table_type が空の文字列ではない場合、検索する型に ついてカンマで区切った値のリストを指定する必要があります。 各値は、シングルクオート(')で括るかまたは括らない形で指定可能です。 例えば、'TABLE','VIEW' または TABLE, VIEW となります。 データソースが指定したテーブル型をサポートしていない場合、 odbc_tables() はその型についていかなる結果も 返しません。

戻り値

情報を含んでいる ODBC 結果 ID を返します。 失敗した場合に false を返します。

結果セットのカラムは次のようになります。

  • TABLE_CAT
  • TABLE_SCHEM
  • TABLE_NAME
  • TABLE_TYPE
  • REMARKS
ドライバは追加のカラムを返すことが出来ます。

結果セットは TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME でソートされます。

変更履歴

バージョン 説明
8.0.0 schema, table, types は、nullable になりました。

例1 カタログのテーブル一覧を表示する

<?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] =>
)

参考

add a note

User Contributed Notes 4 notes

up
2
liquidicee at hotmail dot com
23 years ago
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
up
0
narcomweb at wanadoo dot fr
18 years ago
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.
up
0
iggvopvantoodlwin
20 years ago
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.
up
-7
pmains at ux dot com
18 years ago
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";
}

?>
To Top