Windows 64 Caveats with ODBC
I'm pretty sure PHP only accesses the 32-bit ODBC stuff on Windows 64. Therefore, you will need to configure your ODBC DSNs via the Data Source Administrator found at:
/WINDOWS/SysWOW64/odbcad32.exe
CIV. Unified ODBC 関数
導入
通常の ODBC サポートに加えて、PHP の Unified ODBC 関数では、各々の API を実装するために ODBC API のセマンティックスを借用する複数の データベースにアクセスすることが可能です。ほとんど同じ複数のデータベース ドライバを維持管理する代わりに、これらのドライバは単一の ODBC 関数セットに 統合されています。
以下のデータベースが Unified ODBC でサポートされています。 Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere
注意: 上記のデータベースと接続する際に ODBC は使用されません。関数は ODBC 関数として同じ名前と構文を共有していますが、これらのデータベース の専用 API を使用します。この例外は、iODBC です。PHP を iODBC サポート を付けて構築した場合、全ての ODBC 互換のドライバを PHP アプリケーション ドライバが使用可能となります。iODBC は、OpenLink Software により管理されています。 iODBC に関する詳細および HOWTO を http://www.iodbc.org/ で参照可能です。
要件
サポートされるデータベースにアクセスするためには、 所定のライブラリがインストールされている必要があります。
インストール手順
- --with-adabas[=DIR]
Adabas D サポートを有効にします。DIR はAdabas をインストールしたディレクトリで、 /usr/local がデフォルトです。
- --with-sapdb[=DIR]
SAP DB サポートを有効にします。DIR は SAP DB のベースインストールディレクトリで、 /usr/local がデフォルトです。
- --with-solid[=DIR]
Solidサポートを有効にします。DIR は Solid のベースインストールディレクトリで、 /usr/local/solid がデフォルトです。
- --with-ibm-db2[=DIR]
IBM DB2 サポートを有効にします。DIR は IBM DB2 のベースインストールディレクトリで、 /home/db2inst1/sqllib がデフォルトです。
- --with-empress[=DIR]
Empress サポートを有効にします。DIR は Empress のベースインストールディレクトリで、 $EMPRESSPATH がデフォルトです。PHP 4 以降、 このオプションは Empress バージョン 8.60 以降のみをサポートします。
- --with-empress-bcs[=DIR]
Empress ローカルアクセスサポートを有効にします。DIR は Empress の ベースインストールディレクトリで、$EMPRESSPATH がデフォルトです。PHP 4 以降、 このオプションは Empress バージョン 8.60 以降のみをサポートします。
- --with-birdstep[=DIR]
Birdstep サポートを有効にします。DIR は Birdstep のベースインストールディレクトリで、 /usr/local/birdstep がデフォルトです。
- --with-custom-odbc[=DIR]
ユーザ定義の ODBC サポートを有効にします。DIR は ODBC のベースインストールディレクトリで、 /usr/local がデフォルトです。 CUSTOM_ODBC_LIBS が定義され、odbc.h がインクルードパスに あることを確認してください。 例えば、QNX上 の Sybase SQL Anywhere 5.5.00 では、configure スクリプトを実行する 前に以下を定義する必要があります。 CPPFLAGS="-DODBC_QNX -DSQLANY_BUG" LDFLAGS=-lunix CUSTOM_ODBC_LIBS="-ldblib -lodbc".
- --with-iodbc[=DIR]
iODBC サポートを有効にします。DIR は iODBC のベースインストールディレクトリで、 /usr/local がデフォルトです。
- --with-esoob[=DIR]
Easysoft OOB サポートを有効にします。DIR は OOB のベースインストールディレクトリで、 /usr/local/easysoft/oob/client がデフォルトです。
- --with-unixODBC[=DIR]
unixODBC サポートを有効にします。DIR は unixODBC のベースインストールディレクトリで、 /usr/local がデフォルトです。
- --with-openlink[=DIR]
OpenLink ODBC サポートを有効にします。DIR は OpenLink のベースインストールディレクトリで、 /usr/local がデフォルトです。 これは、iODBC と同じものです。
- --with-dbmaker[=DIR]
DBMaker サポートを有効にします。DIR は DBMaker のベースインストールディレクトリで、 デフォルトは最新版の DBMaker がインストールされている場所 (例えば /home/dbmaker/3.6)です。
PHP 3 で unified ODBC サポートを無効にするには、 --disable-unified-odbc を configure 実行時に 指定します。このオプションは、iODBC, Adabas, Solid, Velocis custom ODBC インターフェイスを 有効にしている場合のみ適用可能です。
Windows 版の PHP には この拡張モジュールのサポートが組み込まれています。これらの関数を使用 するために拡張モジュールを追加でロードする必要はありません。
実行時設定
php.ini の設定により動作が変化します。
表 1. Unified ODBC 設定オプション
| 名前 | デフォルト | 変更の可否 | 変更履歴 |
|---|---|---|---|
| odbc.default_db * | NULL | PHP_INI_ALL | |
| odbc.default_user * | NULL | PHP_INI_ALL | |
| odbc.default_pw * | NULL | PHP_INI_ALL | |
| odbc.allow_persistent | "1" | PHP_INI_SYSTEM | |
| odbc.check_persistent | "1" | PHP_INI_SYSTEM | |
| odbc.max_persistent | "-1" | PHP_INI_SYSTEM | |
| odbc.max_links | "-1" | PHP_INI_SYSTEM | |
| odbc.defaultlrl | "4096" | PHP_INI_ALL | |
| odbc.defaultbinmode | "1" | PHP_INI_ALL |
注意: * マークがついているエントリは未実装です。
以下に設定ディレクティブに関する 簡単な説明を示します。
- odbc.default_db string
odbc_connect() または odbc_pconnect()でODBCデータソースが 指定されない場合に使用される ODBC データソース。
- odbc.default_user string
odbc_connect() または odbc_pconnect() で名前が指定されない場合 に使用される名前。
- odbc.default_pw string
odbc_connect() または odbc_pconnect() でパスワードが指定されない場合 に使用されるパスワード。
- odbc.allow_persistent boolean
持続的 ODBC 接続を許可するかどうか。
- odbc.check_persistent boolean
再利用する前に接続が有効であることを確認します。
- odbc.max_persistent integer
プロセス毎の持続的 ODBC 接続の最大数。
- odbc.max_links integer
プロセス毎の持続的接続を含む ODBC 接続の最大数。
- odbc.defaultlrl integer
LONG フィールドの処理。変数に返されるバイト数を指定します。
integerを使用する際、 その値はバイト単位で測られます。 この FAQ に記載された 短縮表記を使用することも可能です。
- odbc.defaultbinmode integer
バイナリデータの処理モード。
リソース型
リソース型は定義されていません。
定義済み定数
以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。
- ODBC_TYPE (integer)
- ODBC_BINMODE_PASSTHRU (integer)
- ODBC_BINMODE_RETURN (integer)
- ODBC_BINMODE_CONVERT (integer)
- SQL_ODBC_CURSORS (integer)
- SQL_CUR_USE_DRIVER (integer)
- SQL_CUR_USE_IF_NEEDED (integer)
- SQL_CUR_USE_ODBC (integer)
- SQL_CONCURRENCY (integer)
- SQL_CONCUR_READ_ONLY (integer)
- SQL_CONCUR_LOCK (integer)
- SQL_CONCUR_ROWVER (integer)
- SQL_CONCUR_VALUES (integer)
- SQL_CURSOR_TYPE (integer)
- SQL_CURSOR_FORWARD_ONLY (integer)
- SQL_CURSOR_KEYSET_DRIVEN (integer)
- SQL_CURSOR_DYNAMIC (integer)
- SQL_CURSOR_STATIC (integer)
- SQL_KEYSET_SIZE (integer)
- SQL_CHAR (integer)
- SQL_VARCHAR (integer)
- SQL_LONGVARCHAR (integer)
- SQL_DECIMAL (integer)
- SQL_NUMERIC (integer)
- SQL_BIT (integer)
- SQL_TINYINT (integer)
- SQL_SMALLINT (integer)
- SQL_INTEGER (integer)
- SQL_BIGINT (integer)
- SQL_REAL (integer)
- SQL_FLOAT (integer)
- SQL_DOUBLE (integer)
- SQL_BINARY (integer)
- SQL_VARBINARY (integer)
- SQL_LONGVARBINARY (integer)
- SQL_DATE (integer)
- SQL_TIME (integer)
- SQL_TIMESTAMP (integer)
- SQL_TYPE_DATE (integer)
- SQL_TYPE_TIME (integer)
- SQL_TYPE_TIMESTAMP (integer)
- SQL_BEST_ROWID (integer)
- SQL_ROWVER (integer)
- SQL_SCOPE_CURROW (integer)
- SQL_SCOPE_TRANSACTION (integer)
- SQL_SCOPE_SESSION (integer)
- SQL_NO_NULLS (integer)
- SQL_NULLABLE (integer)
- SQL_INDEX_UNIQUE (integer)
- SQL_INDEX_ALL (integer)
- SQL_ENSURE (integer)
- SQL_QUICK (integer)
- 目次
- odbc_autocommit -- 自動コミットの動作をオンまたはオフにする
- odbc_binmode -- バイナリカラムデータを処理する
- odbc_close_all -- 全ての ODBC 接続を閉じる
- odbc_close -- ODBC 接続を閉じる
- odbc_columnprivileges -- カラムおよび付随する権限のリストを取得する際に使用する結果 ID を返す
- odbc_columns -- 指定したテーブルにあるカラム名のリストを取得する
- odbc_commit -- ODBC トランザクションをコミットする
- odbc_connect -- データソースに接続する
- odbc_cursor -- カーソル名を得る
- odbc_data_source -- 現在の接続についての情報を返す
- odbc_do -- odbc_exec()の同義語
- odbc_error -- 直近のエラーコードを得る
- odbc_errormsg -- 直近のエラーメッセージを得る
- odbc_exec -- SQL文を準備し、実行する
- odbc_execute -- プリペアドステートメントを実行する
- odbc_fetch_array -- 連想配列として結果の行を取得する
- odbc_fetch_into -- 1 行文の結果を配列に取り込む
- odbc_fetch_object -- オブジェクトとして結果の行を取得する
- odbc_fetch_row -- 行を取り込む
- odbc_field_len -- フィールドの長さ(精度)を得る
- odbc_field_name -- カラム名を得る
- odbc_field_num -- カラム番号を返す
- odbc_field_precision -- odbc_field_len() の同義語
- odbc_field_scale -- フィールドの精度を得る
- odbc_field_type -- フィールドのデータ型を返す
- odbc_foreignkeys -- 指定したテーブルの外部キーのリストまたは指定したテーブルの主キーを 参照する他のテーブルの外部キーのリストを返す
- odbc_free_result -- 結果を保持するリソースを開放する
- odbc_gettypeinfo -- データソースによりサポートされるデータ型に関する情報を有する結果 ID を返す
- odbc_longreadlen -- LONG カラムを処理する
- odbc_next_result -- 複数の結果が利用可能などうか確認する
- odbc_num_fields -- 結果のカラム数を返す
- odbc_num_rows -- 結果における行数を返す
- odbc_pconnect -- 持続的なデータベース接続を開く
- odbc_prepare -- 実行用に文を準備する
- odbc_primarykeys -- テーブルの主キーを有するカラムの名前を取得する際に使用可能な結果 ID を返す
- odbc_procedurecolumns -- プロシージャへのパラメータに関する情報を取得する
- odbc_procedures -- 指定したデータソースに保存されているプロシージャのリストを取得する
- odbc_result_all -- HTML テーブルとして結果を出力する
- odbc_result -- 結果データを得る
- odbc_rollback -- トランザクションをロールバックする
- odbc_setoption -- ODBC の設定を変更する
- odbc_specialcolumns -- テーブルのレコードを特定する最適なカラムの組合せまたはレコードの 値がトランザクションにより更新される際に自動的に更新されるカラムを 返す
- odbc_statistics -- テーブルに関する統計情報を取得する
- odbc_tableprivileges -- 各テーブルのリストおよび関連する権限のリストを取得する
- odbc_tables -- 指定したデータソースに保存されたテーブルの名前のリストを取得する
I had big performance problems retrieving data form MS SQL Server with odbc only when the query was unsing a join.
I found out, that I had to connect with the cursor-type "SQL_CUR_USE_ODBC" and everything was ok:
$conn = odbc_connect("ShopLive", 'shop', 'xxx', SQL_CUR_USE_ODBC);
I found this to be a perfect alternative to the MaxDB special drivers of version 7.5.00. Just weren't that easy to install on *nix. Windows seems fine. Anyway The ODBC is a perfect alternative for connecting the SAPDB/MaxDB towards PHP.
Installation guide for the odbc alternative (instead of the MAXDB-php driver) can be found here:
http://maxdb.yapabout.com/viewtopic.php?t=21
If you receive an error stating "Connection is busy with results for another hstmt, SQL state S1000 in SQLExecDirect" try opening your odbc connection using the SQL_CURSOR_FORWARD_ONLY option
<?php
$db_link = odbc_connect($dsn, $username, $password, SQL_CURSOR_FORWARD_ONLY)
or die('Error connecting to server. Server says: '.htmlspecialchars(odbc_errormsg()));
?>
In response to Holger's comment about using @@identity:
Be carefull. If the table you're inserting into has a trigger that also inserts into another table that has an identity column you'll get the key of that other table! use scope_identity() instead of @@identity
I searched for the solution of why odbc connection of a network remote drive under Windows + Apache 2.0.X, cannot give the query, but seems no one provides the solution.
In fact, it is very simple.
Go to Control Panal -> Services;
Find and double click "Apache2";
In the page of "Log On", choose Log on as "This account" and give an account in the web server system which have the right to control the network remote drive;
Finally, restart Apache, and that's it.
Ever wonder why you're experiencing really slow data retrieval times using IBM DB2 Universal Database for Linux, UNIX, and Windows? The default cursor type used by Unified ODBC is not supported by DB2, so it gets downgraded to a forward-only cursor -- and that negotiation occurs with every row fetch.
One way to force your PHP applications to use forward-only cursors is to modify your DB2 client configuration with a handy CLI patch2 setting value of 6:
$ db2 UPDATE CLI CONFIGURATION FOR SECTION dbname USING patch2 6
You have to update this client setting on the same machine on which you are running the PHP application. This works on Windows operating systems as well as on Linux & UNIX operating systems.
I ran a few basic benchmarks (fetch 10,000 rows consisting of 3 INTEGER columns from a remote database server) and concluded that this setting can make a major difference to your application speed:
Without CLI patch2 setting: ~22 seconds
With CLI patch2 setting: ~ 1.75 seconds
Note that the drawback of using this patch setting (or any other method of using forward-only cursors) makes odbc_num_rows() always return "-1" for the number of rows affected by a SELECT statement.
If the bundled ODBC library stumbles on some field formats (like some REAL from Pervasive.SQL), have a look at http://odbtp.sourceforge.net/.
After many headaches, I have adoped odbtp: it's a very solid library and best of all it's not tied to a particular OS.
On my search for a function to retriew the NewID of an inserted row wich has an autoincrement I found this solution like the mysql_insert_id for an ODBC connection to MS-Access :
<?
// make your connection below
$Connection = odbc_connect(...);
$Result = odbc_exec($Connection, "select @@identity");
$NewID = odbc_result($Result, 1);
odbc_free_result($Result);
// make here all what you want with the NewID
odbc_close($Connection);
?>
In my mind this should also work with MS-SQL-Server and with Sybase - via ODBC and direct (mssql_.../sybase_...).
HTH ...
Regards
Holger
