PHP 8.3.4 Released!

ibase_connect

(PHP 5, PHP 7 < 7.4.0)

ibase_connectデータベースへの接続をオープンする

説明

ibase_connect(
    string $database = ?,
    string $username = ?,
    string $password = ?,
    string $charset = ?,
    int $buffers = ?,
    int $dialect = ?,
    string $role = ?,
    int $sync = ?
): resource

Firebird/InterBase サーバーへの接続を確立します。

同じ引数で ibase_connect() が再度コールされた場合、 新規のリンクは作成されず、代わりに既にオープンされているリンクの リンク ID が返されます。 ibase_close() を明示的にコールしない限り、 サーバーへのリンクはスクリプトの実行終了時に閉じられます。

パラメータ

database

databaseは接続するサーバー上のデータベース ファイルへの正しいパスである必要があります。ローカルなサーバーへの 接続でない場合、使用する接続プロトコルに応じてこの引数の前に 'hostname:' (TCP/IP)、'hostname/port:' (カスタム TCP ポート上での interbase サーバーでの TCP/IP)、'//hostname/' (NetBEUI) のどれかをつける必要があります。

username

ユーザー名。php.ini ディレクティブ ibase.default_user で設定します。

password

username のパスワード。 php.ini ディレクティブ ibase.default_password で設定します。

charset

charset はデータベースに関するデフォルトの文字セットです。

buffers

buffers はサーバー側のキャッシュに確保されるデータベースバッファの数です。0 または省略された場合、サーバーはデフォルト値を用います。

dialect

dialect は、接続時に実行される全ての命令に 関する SQL 方言のデフォルト値を選択し、デフォルトではクライアント ライブラリでサポートされる方言のうち、最高位のものになります。

role

InterBase 5 以降でのみ有効です。

sync

戻り値

成功した場合に Firebird/InterBase リンク ID、エラー時に false を返します。

エラー / 例外

この関数を使用して ibase_query() をコールした後に "arithmetic exception, numeric overflow, or string truncation. Cannot transliterate character between character sets" のようなエラーが発生した場合 (たとえばアクセント記号つきの文字を使用した場合などに発生します)、 文字セットを指定する必要があります (例: ISO8859_1 あるいは現在の文字セット)。

例1 ibase_connect() の例

<?php
$host
= 'localhost:/path/to/your.gdb';

$dbh = ibase_connect($host, $username, $password);
$stmt = 'SELECT * FROM tblname';
$sth = ibase_query($dbh, $stmt);
while (
$row = ibase_fetch_object($sth)) {
echo
$row->email, "\n";
}
ibase_free_result($sth);
ibase_close($dbh);
?>

参考

  • ibase_pconnect() - InterBase データベースへの持続的接続をオープンする
  • ibase_close() - InterBase データベースへの接続を閉じる

add a note

User Contributed Notes 7 notes

up
0
simon at thorndancer dot com
20 years ago
I found it difficult to get anything working using the current documentation.
While logic would suggest that the format in the docs should work (being based on all the
syntax of other standard database access functions) I had problem after problem. Finally I
found some comments on a newgroup that helped, and this is the result.

This is actual production code (not a mere example). It works.
Don't ask me why it is so different from the official docs.

if ( ibase_connect( $db_filespec,$db_user,$db_pass_word) )
{
$query = "SELECT c.id, c.title, c.description ";
$query .= "FROM collections c ";
$query .= "WHERE (c.id = $page_id) ";

$result = ibase_query( $query );

$row_array = ibase_fetch_row($result);

$row_fetched = !empty($row_array);

if ($row_fetched)
{
// Extract the row into variables

$collection_id = $row_array[0];
$edit_title = $row_array[1];
$edit_desc = $row_array[2];

// Standardise the record contents

$collection_id = $collection_id + 0; // force type to numeric
}

// Wrap up the database side of things

ibase_commit(); // note parenthesis but no parameters.

ibase_close; // note total lack of parenthesis and parameters !
}

You'll notice that there is no reference to a database handle in this code.
It seems to be implicit when dealing with interbase functions. Certainly, I couldn't
get the code to work when using it explicitly. This makes the documentation
very hard to work with, and any reference to a handle needs to be ignored.

Important: ibase_close doesn't work for me if I put parenthesis after it.
eg:
ibase_close; // works
ibase_close(); // fails
ibase_close($db); // fails

Note: the line "if ($row_fetched)" could be replaced with "while ($row_fetched)"
if you need to process more than one record.

All that said, Interbase is a fantastic database to work with and IMHO a much
better choice than something like PostgreSQL if you need to move up from MySQL
to something more industrial strength. Just be sure to RTFM.
up
0
anthony at trams dot com
23 years ago
If you get a message like: 'Dynamic SQL Error SQL error code = -901 feature is not supported', this may be caused by InterBASE client 6.01 accessing a server running 5.x.

Make sure the dialect value is set to '1', like the following:

ibase_connect ('localhost:/usr/db/db.gdb', 'SYSDBA', 'masterkey', 'ISO8859_1', '100', '1' ) ;

That last value is the dialect, but you need the two preceding also, do avoid errors.


Anthony
up
-1
fwrk at bk dot ru
9 years ago
If you want to connect to interbase server runned on custom port (not default port 3050), you can specify it in "database" parameter in this way: 'hostname/port:' (TCP/IP with custom port).
Examples:
$dbh = ibase_connect('example.com/11099:dbalias', $username, $password);
$dbh = ibase_connect('example.com/11099:var/data/myibase.fdb', $username, $password);
$dbh = ibase_connect('example.com/11099:C:\\data\\mywinibase.fdb', $username, $password);
up
-5
ag at dividi dot de
18 years ago
With php5 on Windows i couldnt connect to a firebird database with following error :

Warning: ibase_connect(): Unable to complete network request to host "localhost"
. Failed to locate host machine. Undefined service gds_db/tcp. in C:\\wamp\\php\\ibase.php on line 5

the connect command was "$dbh = ibase_connect ("localhost:path_to_file.fdb", $username, $password);"

The solution is to insert the line
gds_db 3050/tcp #firebird Database
into the windows services file %WINDIR%\\system32\\drivers\\etc\\services
up
-6
Robert Silvia
19 years ago
Just to update Simon's note...

Things must have been fixed since Simon wrote that note.
With PHP5 the following example works flawlessly:

$strconnect = 'localhost:/opt/firebird/examples/employee.fdb';

if (!($db=ibase_connect($strconnect, 'sysdba', 'yourpass', 'ISO8859_1', 0, 1)))
die('Could not connect: ' . ibase_errmsg());

$sql = "SELECT PO_NUMBER, ORDER_STATUS, ITEM_TYPE FROM SALES";
$cursor = ibase_query($sql);
$row_array = ibase_fetch_row($cursor);

print_r($row_array);

ibase_free_result($cursor);
ibase_close($db);

Also you CAN get away with the following connection string:
ibase_connect('/opt/firebird/examples/employee.fdb', 'sysdba', 'yourpass')
up
-9
fb dot bird at gmail dot com
16 years ago
Remember for different locations
of php.ini file:

..\xampp\php\php.ini
..\xampp\apache\bin\php.ini
..\xampp\php\php4\php.ini

Please do not confuse this
and know location _loaded_ *.ini-file!
up
-16
sam at intranex.ru
19 years ago
If you get a sloooow work of ibase_connect on Win2003, try to update your FireBird to version 1.5 or above, and your InterBase to version 7.1. Or move back to Win2000 :)
To Top