This example have 2 problems my be the autor writes it to fast but in the first case use one var for define user pass and and the use other one for call them and in secon step use comas after the ;
$db = '/path/to/database.gdb';
$user = 'username';
$password = 'password';
$res = ibase_connect($db,$dbuser,$dbpass) or die("<br>" . ibase_errmsg());
// Query
$sql="SELECT * FROM table;"
XLII. Firebird/InterBase 関数
導入
Firebird/InterBase は ANSI SQL-92 の多くの機能をサポートする リレーショナルデータベースで、Linux・Windows その他多くの Unix プラットフォーム上で動作します。Firebird/InterBase は 優れた同時実行性・パフォーマンス、そしてストアドプロシージャや トリガでの強力な言語サポートを提供します。1981 年以降、このデータベースは さまざまな名前の製品として使用されています。
InterBase はこの RDBMS のクローズドソース版の名前で、Borland/Inprise によって開発されました。InterBase についての詳細な情報は http://www.borland.com/interbase/ にあります。
Firebird は C・C++プログラムやテクニカルアドバイザやサポーターたちによる 商業的に独立したプロジェクトです。Inprise Corp(現在は Borland Software Corp という名で知られています)が 2000 年 7 月 25 日に InterBase Public License v.1.0 の下で公開したソースコードをもとにして、 マルチプラットフォームなリレーショナルデータベースの開発と機能拡張を 行っています。Firebird についての詳細な情報は http://www.firebirdsql.org/ にあります。
インストール手順
PHP で InterBase サポートを有効にするには、 --with-interbase[=DIR] を指定して 設定を行います。ただし、DIR は InterBase のベースインストールディレクトリで、 デフォルトは /usr/interbase です。
Win32 ユーザへの注意: このモジュールをWindows環境で有効にするには、 gds32.dllをPHP/Win32バイナリパッケージのDLLフォ ルダから使用するWindowsマシンのSYSTEM32フォルダ(例: C:\WINNT\SYSTEM32 または C:\WINDOWS\SYSTEM32) にコピーする必要があります。 InterBase データベースサーバを PHP を実行しているのと同じマシンに インストールしている場合、この DLL はすでにあるはずです。この場合、 DLL フォルダから gds32.dll をコピーする必要は ありません。
実行時設定
php.ini の設定により動作が変化します。
表 1. InterBase設定オプション
| 名前 | デフォルト | 変更の可否 | 変更履歴 |
|---|---|---|---|
| ibase.allow_persistent | "1" | PHP_INI_SYSTEM | |
| ibase.max_persistent | "-1" | PHP_INI_SYSTEM | |
| ibase.max_links | "-1" | PHP_INI_SYSTEM | |
| ibase.default_db | NULL | PHP_INI_SYSTEM | PHP 5.0.0 以降で使用可能です。 |
| ibase.default_user | NULL | PHP_INI_ALL | |
| ibase.default_password | NULL | PHP_INI_ALL | |
| ibase.default_charset | NULL | PHP_INI_ALL | PHP 5.0.0 以降で使用可能です。 |
| ibase.timestampformat | "%Y-%m-%d %H:%M:%S" | PHP_INI_ALL | |
| ibase.dateformat | "%Y-%m-%d" | PHP_INI_ALL | |
| ibase.timeformat | "%H:%M:%S" | PHP_INI_ALL |
以下に設定ディレクティブに関する 簡単な説明を示します。
- ibase.allow_persistent boolean
Firebird/InterBase 持続的な接続 を許可するかどうか。
- ibase.max_persistent integer
プロセスごとの、Firebird/InterBase の持続的接続の最大数。 この数を超えた場合、ibase_pconnect() による新しい接続は 持続的ではない接続になります。
- ibase.max_links integer
プロセスごとの Firebird/InterBase の接続の最大数。持続的な接続の数も 含みます。
- ibase.default_db string
データベース名を指定せずに ibase_[p]connect() がコールされた場合に 接続するデフォルトのデータベース。この値が設定されており、かつ SQL セーフモードが有効な場合、このデータベース以外への接続は一切許可されません。
- ibase.default_user string
ユーザ名を指定せずにデータベースに接続した際に使用されるユーザ名。
- ibase.default_password string
パスワードを指定せずにデータベースに接続した際に使用されるパスワード。
- ibase.default_charset string
文字セットを指定せずにデータベースに接続した際に使用される文字セット。
- ibase.timestampformat string
- ibase.dateformat string
- ibase.timeformat string
これらのディレクティブは日付や時間のフォーマットを指定します。 結果セットの中の日付や時間のデータ、そしてパラメータとしてバインドする 日付や時間のデータに適用されます。
定義済み定数
以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。
トランザクションの挙動を設定するため、以下の定数を ibase_trans() に渡すことが可能です。
表 2. Firebird/InterBase トランザクションフラグ
| 定数 | 説明 |
|---|---|
| IBASE_DEFAULT | デフォルトのトランザクション設定が使用されます。デフォルト設定は クライアントライブラリによって決定され、たいていの場合は IBASE_WRITE|IBASE_CONCURRENCY|IBASE_WAIT です。 |
| IBASE_READ | 読み込み専用のトランザクションを開始します。 |
| IBASE_WRITE | 読み書き可能なトランザクションを開始します。 |
| IBASE_CONSISTENCY | 分離レベルを 'consistency' にしてトランザクションを開始します。 他のトランザクションによって変更中のテーブルを読み込むことは できません。 |
| IBASE_CONCURRENCY | 分離レベルを 'concurrency' (あるいは 'snapshot') にして トランザクションを開始します。すべてのテーブルへのアクセスが 可能ですが、トランザクションの開始以降に別のトランザクションに よってコミットされた内容を見ることはできません。 |
| IBASE_COMMITTED | 分離レベルを 'read committed' にしてトランザクションを開始します。 このフラグは IBASE_REC_VERSION あるいは IBASE_REC_NO_VERSION とともに使用する必要があります。 トランザクションの開始以降にコミットされた内容についてもアクセス可能と なります。IBASE_REC_NO_VERSION が指定された場合、 最新バージョンのデータのみが読み込み可能となります。 IBASE_REC_VERSION が指定された場合、他の トランザクションで処理が完了していない変更についても読み込むことが できます。 |
| IBASE_WAIT | 衝突が発生した場合に、トランザクションが処理を再試行することを示します。 |
| IBASE_NOWAIT | 衝突が発生した場合に、トランザクションがその場で処理を中断して 処理が失敗することを示します。 |
データの取得方法を指定するために、以下の定数を ibase_fetch_row()、 ibase_fetch_assoc() あるいは ibase_fetch_object() に渡すことが可能です。
表 3. Firebird/InterBase フェッチフラグ
| 定数 | 説明 |
|---|---|
| IBASE_FETCH_BLOBS | 過去との互換性を保持するため、IBASE_TEXT としても使用可能です。BLOB データを読み込む際に、BLOB ID ではなく 直接データそのものを読み込みます。 |
| IBASE_FETCH_ARRAYS | 配列をインラインで読み込みます。指定しなかった場合は、配列の ID を返します。配列 ID は INSERT 操作への引数としてのみ使用可能で、 配列 ID を処理できる関数は現在存在しません。 |
| IBASE_UNIXTIME | 日付や時刻のフィールド値を、文字列ではなく UNIX タイムスタンプ (1-Jan-1970 0:00 UTC からの経過秒数)で返します。 1970 年より前の日付に対してこれを使用すると、環境によっては 問題を引き起こす可能性があります。 |
要求内容やオプションを指定するために、以下の定数を サービス API 関数(ibase_server_info(), ibase_db_info(), ibase_backup(), ibase_restore() および ibase_maintain_db())に渡すことが可能です。 これらのオプションの意味については Firebird/InterBase のマニュアルを 参照ください。
- IBASE_BKP_IGNORE_CHECKSUMS
- IBASE_BKP_IGNORE_LIMBO
- IBASE_BKP_METADATA_ONLY
- IBASE_BKP_NO_GARBAGE_COLLECT
- IBASE_BKP_OLD_DESCRIPTIONS
- IBASE_BKP_NON_TRANSPORTABLE
- IBASE_BKP_CONVERT
ibase_backup() へのオプション
- IBASE_RES_DEACTIVATE_IDX
- IBASE_RES_NO_SHADOW
- IBASE_RES_NO_VALIDITY
- IBASE_RES_ONE_AT_A_TIME
- IBASE_RES_REPLACE
- IBASE_RES_CREATE
- IBASE_RES_USE_ALL_SPACE
ibase_restore() へのオプション
- IBASE_PRP_PAGE_BUFFERS
- IBASE_PRP_SWEEP_INTERVAL
- IBASE_PRP_SHUTDOWN_DB
- IBASE_PRP_DENY_NEW_TRANSACTIONS
- IBASE_PRP_DENY_NEW_ATTACHMENTS
- IBASE_PRP_RESERVE_SPACE
- IBASE_PRP_RES_USE_FULL
- IBASE_PRP_RES
- IBASE_PRP_WRITE_MODE
- IBASE_PRP_WM_ASYNC
- IBASE_PRP_WM_SYNC
- IBASE_PRP_ACCESS_MODE
- IBASE_PRP_AM_READONLY
- IBASE_PRP_AM_READWRITE
- IBASE_PRP_SET_SQL_DIALECT
- IBASE_PRP_ACTIVATE
- IBASE_PRP_DB_ONLINE
- IBASE_RPR_CHECK_DB
- IBASE_RPR_IGNORE_CHECKSUM
- IBASE_RPR_KILL_SHADOWS
- IBASE_RPR_MEND_DB
- IBASE_RPR_VALIDATE_DB
- IBASE_RPR_FULL
- IBASE_RPR_SWEEP_DB
ibase_maintain_db() へのオプション
- IBASE_STS_DATA_PAGES
- IBASE_STS_DB_LOG
- IBASE_STS_HDR_PAGES
- IBASE_STS_IDX_PAGES
- IBASE_STS_SYS_RELATIONS
ibase_db_info() へのオプション
- IBASE_SVC_SERVER_VERSION
- IBASE_SVC_IMPLEMENTATION
- IBASE_SVC_GET_ENV
- IBASE_SVC_GET_ENV_LOCK
- IBASE_SVC_GET_ENV_MSG
- IBASE_SVC_USER_DBPATH
- IBASE_SVC_SVR_DB_INFO
- IBASE_SVC_GET_USERS
ibase_server_info() へのオプション
- 目次
- ibase_add_user -- セキュリティデータベースにユーザを追加する(IB6 以降のみ)
- ibase_affected_rows -- 直近のクエリで変更された行の数を返す
- ibase_backup -- サービスマネージャのバックアップタスクを起動し、すぐに結果を返す
- ibase_blob_add -- 生成された blob にデータを追加する
- ibase_blob_cancel -- blob の生成を取り消す
- ibase_blob_close -- blob を閉じる
- ibase_blob_create -- データを追加するために blob を生成する
- ibase_blob_echo -- ブラウザに blob の内容を出力する
- ibase_blob_get -- オープンした blob から len バイト分のデータを取得する
- ibase_blob_import -- blob を生成し、ファイルをコピーし、閉じる
- ibase_blob_info -- blob の長さと他の便利な情報を返す
- ibase_blob_open -- データの一部を取得するために blob をオープンする
- ibase_close -- InterBase データベースへの接続を閉じる
- ibase_commit_ret -- トランザクションを閉じずにコミットする
- ibase_commit -- トランザクションをコミットする
- ibase_connect -- InterBase データベースへの接続をオープンする
- ibase_db_info -- データベースについての統計情報を要求する
- ibase_delete_user -- セキュリティデータベースからユーザを削除する(IB6 以降のみ)
- ibase_drop_db -- データベースを削除する
- ibase_errcode -- エラーコードを返す
- ibase_errmsg -- エラーメッセージを返す
- ibase_execute -- 準備されたクエリを実行する
- ibase_fetch_assoc -- クエリの結果から、行を連想配列として取得する
- ibase_fetch_object -- InterBase データベースからオブジェクトを得る
- ibase_fetch_row -- InterBase データベースから 1 行分の結果を取得する
- ibase_field_info -- フィールドに関する情報を得る
- ibase_free_event_handler -- 登録済みのイベントハンドラをキャンセルする
- ibase_free_query -- 準備されたクエリにより確保されたメモリを解放する
- ibase_free_result -- 結果セットを解放する
- ibase_gen_id -- 指定した名前のジェネレータをひとつ加算し、その新しい値を返す
- ibase_maintain_db -- データベースサーバでメンテナンスコマンドを実行する
- ibase_modify_user -- セキュリティデータベースのユーザを変更する(IB6 以降のみ)
- ibase_name_result -- 結果セットに名前を割り当てる
- ibase_num_fields -- 結果セットにおけるフィールド数を得る
- ibase_num_params -- プリペアドクエリのパラメータ数を返す
- ibase_param_info -- プリペアドクエリのパラメータに関する情報を返す
- ibase_pconnect -- InterBase データベースへの持続的接続をオープンする
- ibase_prepare -- 後でパラメータのバインド及び実行を行うためにクエリを準備する
- ibase_query -- InterBase データベースでクエリを実行する
- ibase_restore -- サービスマネージャのリストアタスクを起動し、すぐに結果を返す
- ibase_rollback_ret -- トランザクションを閉じずにロールバックする
- ibase_rollback -- トランザクションをロールバックする
- ibase_server_info -- データベースサーバについての情報を要求する
- ibase_service_attach -- サービスマネージャに接続する
- ibase_service_detach -- サービスマネージャとの接続を切断する
- ibase_set_event_handler -- イベントが発生した際にコールされるコールバック関数を登録する
- ibase_timefmt -- クエリから返される timestamp、data、time 型カラムのフォーマットを設定する
- ibase_trans -- トランザクションを開始する
- ibase_wait_event -- データベースでイベントが発生するのを待つ
Here is a minimalistic code example. Be sure to create an user and a database in order to make it work.
<?php
// Minimalistic code example
// Connection
$db = '/path/to/database.gdb';
$user = 'username';
$password = 'password';
$res = ibase_connect($db,$dbuser,$dbpass) or die("<br>" . ibase_errmsg());
// Query
$sql="SELECT * FROM table;"
$result=ibase_query($res,$sql) or die(ibase_errmsg());
while($row=ibase_fetch_object($result)){
// use $row->FIELDNAME not $row->fieldname
print $row->FIELDNAME;
}
ibase_free_result($result);
// Closing
ibase_close($res) or die("<br>" . ibase_errmsg());
?>
The following code can be used when creating tables in order to get auto incrementing fields:
<?php
// This function generates an autoincrement field, such as MySQL AUTO_INCREMENT.
function generate_autoincrement($tablename,$primarykey){
// * Generator
dbexec('CREATE GENERATOR GEN_' . $tablename . '_PK;');
// * Trigger
dbexec('CREATE TRIGGER INC_' . $primarykey . ' FOR ' . $tablename
. chr(13) . 'ACTIVE BEFORE INSERT POSITION 0'
. chr(13) . 'AS'
. chr(13) . 'BEGIN'
. chr(13) . 'IF (NEW.' . $primarykey . ' IS NULL) THEN'
. chr(13) . 'NEW.' . $primarykey . '= GEN_ID(GEN_' . $tablename . '_PK, 1);'
. chr(13) . 'END');
}
?>
Usage: <?php generate_autoincrement('table','column name'); ?>
Here's an example for getting results back from stored procedure in firebird.
The example make use of the stored procedure in Employee.gdb and the show_langs procedure.
$host = 'localhost:X:/firebird/examples/Employee.gdb';
$username='SYSDBA';
$password='masterkey';
$dbh = ibase_connect ( $host, $username, $password ) or die ("error in db connect");
$stmt="Select * from SHOW_LANGS('SRep',4,'Italy')";
$query = ibase_prepare($stmt);
$rs=ibase_execute($query);
$row = ibase_fetch_row($rs);
echo $row[0];
/* free result */
ibase_free_query($query);
ibase_free_result($rs);
/* close db */
ibase_close($dbh);
?>
It is not possible to use interbase/firebird without initiating transactions. It seems that transactions are not automatically committed or rolled back at the end of a script, so remember to end all interbase enabled scripts with ibase_rollback() or ibase_commit().
Worse is, that if you use ibase_pconnect (recommended), transactions survive from one request to the next. So that if you don't rollback your transaction at the end of the script, another user's request might continue the transaction that the first request opened.
This has two implications:
1) Clicking refresh in your browser won't make you see newer data, because you still watch data from the same transaction.
2) Some php scripts might fail occassionally and not fail in other occasions, depending on with apache server thread and thereby which transaction they start using.
Unfortunately, there is no such thing as
if (ibase_intransaction()) ibase_rollback();
so be sure that ALL your scripts end with an ibase_rollback() or ibase_commit();
If you are using VirtualHosts with Apache, you might find useful the following directive:
php_flag magic_quotes_sybase on
Use it in any VirtualHost and it will be set locally to that VirtualHost without interfering with any global setting.
This is an example:
<VirtualHost 555.666.777.888>
ServerName www.samplehost.com
DirectoryIndex index.php index.htm
php_flag magic_quotes_sybase on
</VirtualHost>
Hello PHP Mania,
i have made a paging for PHP with Interbase...... :)
i hope it usefull and work....:)
it`s a litle bit of example :
<?
$connection = ibase_connect($yourdb, $user, $password);
$filename = BASENAME(__FILE__);
$strsql = "Your SQL";
$result = ibase_query($connection, $strsql);
function ibase_num_rows($query) { //I have pick it from bg_idol@hotmail.com
$i = 0;
while (ibase_fetch_row($query)) {
$i++;
}
return $i;
}
$nrow = ibase_num_rows($result);//sum of row
$strSQL = "your SQL";
$result = ibase_query($connection, $strSQL);
if (!isset($page))
$page = 1;
$$i = 0;
$recperpage = 4;
$norecord = ($page - 1) * $recperpage;
if ($norecord){
$j=0;
while($j < $norecord and list($code, $name)= ibase_fetch_row($result)){
$j++;
}
}
echo "<table>";
while (list($code, $name)= ibase_fetch_row($result) and $i < $recperpage){
?>
<tr>
<td width="5%"><? echo $code; ?></td>
<td><? echo $name; ?></td>
</tr>
<?
$i++;
}
$incr = $page + 1;
if ($page > 1) $decr = $page - 1;
$numOfPage = ceil($nrow/$recperpage);
?>
<tr>
<td colspan="3" align="center"><?if ($page <= 1)
echo "<span>Prev</span>";
else
echo "<a href=".$filename."?page=".$decr.">Prev</a>";
?>
<?if ($page == $numOfPage)
echo "<span>Next</span>";
else
echo "<a href=".$filename."?page=".$incr.">Next</a>";?>
</td>
</tr>
</table>
For those who have problem with returning values from Stored Procedures in PHP-Interbase, I have found a solution. Use a select sentence like this:
select * from sp_prodecure(param, ...)
However, it is important that the procedure has a SUSPEND statement or else the procedure won't return any values.
But the "message length" (see above note) bug that you encounter when you try to execute a procedure should be fixed !
