There seems to be a lot of good documentation
for Linux users compiling PHP with DB2 support,
but decent Windows notes are minimal.
You do not need to install full DB2 clients to get DB2
working with DB2, all you need is the IBM Data
Server Driver for ODBC, CLI, and .NET which is only
16.1 meg.
You can download the driver from here:
Direct Link:
ftp://ftp.software.ibm.com/ps/products/db2/fixes2/englsh-us/
db2_v95/dsdriver/fp2/v9.5fp2_nt32_dsdriver_EN.exe
Home Page:
http://www-01.ibm.com/support/docview.wss?rs=71&uid=swg21287889
This includes both the drivers required and the PHP
dll php_ibm_db2_5.2.2.dll
Once installed the drivers do not setup the correct
path environmental variable,
so add the following to your path:
C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin
Once thats done all should work! No massive
400meg client downloads required.
Whats even better about these drivers is that you
dont need to install them,
you can simply copy the bin directory to any server,
add it to your path and it will just work.
This is great for anyone developing PHP-GTK applications,
I copy the bin directory into my php-gkt2 directory
and execute using the following batch script:
path = %PATH%;.\IBM DATA SERVER DRIVER\bin
php-win.exe %*
This lets me role out lightweight DB2 client desktop
apps that dont have to be installed,
can just be coppied from PC to PC or ran over a
network or from USB stick.
As your only installing the client drivers you wont be
able to catalog databases,
so always use the full connection string. Here is a
quick bit of code to get you started:
$database = 'databasename';
$user = 'user';
$password = 'password';
$hostname = '127.0.0.1';
$port = 50000;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;" .
"HOSTNAME=$hostname;PORT=$port;".
"PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
$query = 'SELECT * FROM TABLE';
$res = db2_prepare($conn, $query);
db2_execute($res);
while ($row = db2_fetch_array($res)) {
print_r($row);
}
LVII. IBM DB2、Cloudscape および Apache Derby 関数
導入
これらの関数により、DB2 Call Level Interface (DB2 CLI) を使用した IBM DB2 Universal Database、IBM Cloudscape および Apache Derby データベースへのアクセスが可能となります。
要件
IBM DB2 Universal Database for Linux・UNIX・Windows、 IBM Cloudscape、Apache Derby に接続するには、 PHP を稼動させるコンピュータ上に IBM DB2 Universal Database client がインストールされていなければなりません。 この拡張モジュールは、DB2 バージョン 8.2 を対象にして開発およびテストが行われています。
IBM DB2 Universal Database for z/OS・iSeries に接続するには、IBM DB2 Connect あるいはそれと同等の DRDA ゲートウェイソフトウェアも必要となります。
Linux あるいは Unix についての要件
これらの関数を使用する前には、PHP 実行ファイルあるいは SAPI を実行するユーザで DB2 インスタンスを指定する必要があります。 php.ini で ibm_db2.instance_name を使用して DB2 インスタンス名を指定するか、 PHP 実行ファイルの起動前に DB2 インスタンスプロファイルを 読み込むことができます。
例えば、db2inst1 という名前の DB2 インスタンスを /home/db2inst1/ に作成した場合には php.ini に以下の行を追加します。
ibm_db2.instance_name=db2inst1 |
bash$ source /home/db2inst1/sqllib/db2profile |
インストール手順
ibm_db2 拡張モジュールをビルドするには、 DB2 アプリケーション開発用のヘッダファイルおよびライブラリが システムにインストールされていなければなりません。 これらのファイルはデフォルトではインストールされないので、 DB2 のインストーラをもう一度起動して このオプションを追加する必要があります。 IBM DB2 Universal Database の サポートサイト にてフリーで公開されている DB2 Application Development Client に、これらのヘッダファイルが含まれています。
既に DB2 がインストールされている Linux や Unix 上に DB2 アプリケーション開発用のヘッダおよびライブラリを追加した場合は、 DB2 インスタンス内のヘッダファイルやライブラリに対する シンボリックリンクを更新するため、 コマンド db2iupdt -e を実行しなければなりません。
ibm_db2 は PECL 拡張モジュールなので、 第7章 の手順にしたがって PHP にインストールすることができます。 まず、DB2 ヘッダファイルおよびライブラリの場所を指定するために 次のように configure コマンドを実行します。
bash$ ./configure --with-IBM_DB2=/path/to/DB2 |
実行時設定
php.ini の設定により動作が変化します。
表 1. ibm_db2 設定オプション
| 名前 | デフォルト | 変更可能 | 変更履歴 |
|---|---|---|---|
| ibm_db2.binmode | "1" | PHP_INI_ALL | |
| ibm_db2.instance_name | NULL | PHP_INI_SYSTEM |
以下に設定ディレクティブに関する 簡単な説明を示します。
定義済み定数
以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。
- DB2_BINARY (integer)
バイナリデータをそのままの形式で返します。デフォルトのモードです。
- DB2_CONVERT (integer)
バイナリデータを十六進表現にエンコードし、ASCII 文字列として返します。
- DB2_PASSTHRU (integer)
バイナリデータを NULL 値に変換します。
- DB2_SCROLLABLE (integer)
ステートメントのリソースとして、スクロール可能なカーソルを指定します。 このモードでは結果セット内の行へのランダムアクセスが可能となりますが、 現在は IBM DB2 Universal Database でしかサポートされていません。
- DB2_FORWARD_ONLY (integer)
ステートメントのリソースとして、前進のみ可能なカーソルを指定します。 これはデフォルトのカーソル型であり、 すべてのデータベースサーバでサポートされています。
- DB2_PARAM_IN (integer)
PHP 変数を、ストアドプロシージャの IN パラメータとしてバインドします。
- DB2_PARAM_OUT (integer)
PHP 変数を、ストアドプロシージャの OUT パラメータとしてバインドします。
- DB2_PARAM_INOUT (integer)
PHP 変数を、ストアドプロシージャの INOUT パラメータとしてバインドします。
- DB2_PARAM_FILE (integer)
カラムを直接ファイルにバインドし、そのデータを読み込みます。
- DB2_AUTOCOMMIT_ON (integer)
自動コミットを有効にします。
- DB2_AUTOCOMMIT_OFF (integer)
自動コミットを無効にします。
- DB2_DOUBLE (integer)
変数を、データ型 DOUBLE、FLOAT あるいは REAL にバインドします。
- DB2_LONG (integer)
変数を、データ型 SMALLINT、INTEGER あるいは BIGINT にバインドします。
- DB2_CHAR (integer)
変数を、データ型 CHAR あるいは VARCHAR にバインドします。
- DB2_CASE_NATURAL (integer)
カラム名の大文字小文字を変換せずに返します。
- DB2_CASE_LOWER (integer)
カラム名を小文字に変換して返します。
- DB2_CASE_UPPER (integer)
カラム名を大文字に変換して返します。
リソース型
ibm_db2 拡張モジュールは、接続リソース・ステートメントリソース および結果セットリソースを返します。
- 目次
- db2_autocommit -- データベース接続の AUTOCOMMIT の状態を取得または設定する
- db2_bind_param -- PHP 変数を SQL 文のパラメータにバインドする
- db2_client_info -- DB2 データベースクライアントの情報をプロパティに保持するオブジェクトを返す
- db2_close -- データベース接続を閉じる
- db2_column_privileges -- テーブルのカラムおよび関連する権限情報を含む結果セットを返す
- db2_columns -- テーブルのカラムおよび関連するメタデータを含む結果セットを返す
- db2_commit -- トランザクションをコミットする
- db2_conn_error -- 直近の接続から返された SQLSTATE を含む文字列を返す
- db2_conn_errormsg -- 直近の接続エラーメッセージおよび SQLCODE を返す
- db2_connect -- データベースへの接続を返す
- db2_cursor_type -- ステートメントリソースが使用しているカーソルの型を返す
- db2_exec -- SQL 文を直接実行する
- db2_execute -- プリペアドステートメントを実行する
- db2_fetch_array -- 結果セット内の行を表す、カラム位置をインデックスとする配列を返す
- db2_fetch_assoc -- 結果セット内の行を表す、カラム名をインデックスとする配列を返す
- db2_fetch_both -- 結果セット内の行を表す、 カラム位置およびカラム名の両方をインデックスとする配列を返す
- db2_fetch_object -- 結果セット内の行を表す、カラムをプロパティとするオブジェクトを返す
- db2_fetch_row -- 結果セットポインタを次の行あるいは要求された行に設定する
- db2_field_display_size -- カラムを表示するために必要な最大のバイト数を返す
- db2_field_name -- 結果セット内のカラムの名前を返す
- db2_field_num -- 結果セット内の指定したカラムの位置を返す
- db2_field_precision -- 結果セット内の指定したカラムの精度を返す
- db2_field_scale -- 結果セット内の指定したカラムの位取りを返す
- db2_field_type -- 結果セット内の指定したカラムのデータ型を返す
- db2_field_width -- 結果セット内の指定したカラムの現在値の幅を返す
- db2_foreign_keys -- テーブルの外部キーを含む結果セットを返す
- db2_free_result -- 結果セットに関連付けられたリソースを開放する
- db2_free_stmt -- 指定されたステートメントリソースに関連付けられたリソースを開放する
- db2_next_result -- ストアドプロシージャから、次の結果セットを要求する
- db2_num_fields -- 結果セットに含まれるフィールドの数を返す
- db2_num_rows -- SQL 文によって変更された行の数を返す
- db2_pconnect -- データベースへの持続的接続を返す
- db2_prepare -- 実行する SQL 文を準備する
- db2_primary_keys -- テーブルの主キーを含む結果セットを返す
- db2_procedure_columns -- ストアドプロシージャのパラメータを含む結果セットを返す
- db2_procedures -- データベース内に登録されているストアドプロシージャの一覧を含む結果セットを返す
- db2_result -- 結果セットの行からひとつのカラムを返す
- db2_rollback -- トランザクションをロールバックする
- db2_server_info -- DB2 データベースサーバの情報をプロパティに保持するオブジェクトを返す
- db2_set_option -- 接続リソースあるいはステートメントリソースのオプションを設定する
- db2_special_columns -- テーブルのユニーク行 ID カラムを含む結果セットを返す
- db2_statistics -- インデックスの情報およびテーブルの統計情報を含む結果セットを返す
- db2_stmt_error -- SQL 文が返す SQLSTATE を含む文字列を返す
- db2_stmt_errormsg -- 直近の SQL 文のエラーメッセージを含む文字列を返す
- db2_table_privileges -- データベース内のテーブルおよび関連する権限情報を含む結果セットを返す
- db2_tables -- データベース内のテーブルおよび関連するメタデータを含む結果セットを返す
Spent a lot of time trying to get this to work on a 64bit box. You'll need to make sure you set your db2 client to 64 bit mode! If you didn't set your instance to 64bit, you can run ./db2iupdt -w64 db2instance as root to set it. Hope this helps someone!
The DB/2 Run-Time-Client can be found here:
http://www-1.ibm.com/support/docview.wss?rs=71&uid=swg21255394
Select the 'Runtime Client Installable for Windows' further down the page and download it.
Clients for other platform (incl. 64-Bit Windows) are also available from that page.
If you wish to connect to an iSeries Server, such as an AS/400, there are two options. Installing DB2 Express-C v9.5 and purchasing DB2 Connect Personal Edition for about $500 or getting System I Access for Linux, which is a free download. Please note the server requirements for each of these. For example, we originally went with DB2 Express-C and decided upon Ubuntu 7.10, but learned about System I Access, from an IBM rep, which requires redhat package manager, after the entire server was up and running.
DB2 Express-C
http://www-306.ibm.com/software/data/db2/express/
DB2 Connect Personal
http://www-306.ibm.com/software/data/db2/db2connect/edition-pe.html
System I Access for Linux
http://www-03.ibm.com/systems/i/software/access/index.html
NOTE: System I Access requires redhat package manager.
