Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Firebird/InterBase Functions : ibase_connect

ibase_connect

Open a connection to an InterBase database (PHP 4, PHP 5)
resource ibase_connect ( [string database [, string username [, string password [, string charset [, int buffers [, int dialect [, string role [, int sync]]]]]]]] )

Establishes a connection to an InterBase server.

In case a second call is made to ibase_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling ibase_close().

Parameters

database

The database argument has to be a valid path to database file on the server it resides on. If the server is not local, it must be prefixed with either 'hostname:' (TCP/IP), '//hostname/' (NetBEUI) or 'hostname@' (IPX/SPX), depending on the connection protocol used.

username

The user name. Can be set with the ibase.default_user php.ini directive.

password

The password for username. Can be set with the ibase.default_password php.ini directive.

charset

charset is the default character set for a database.

buffers

buffers is the number of database buffers to allocate for the server-side cache. If 0 or omitted, server chooses its own default.

dialect

dialect selects the default SQL dialect for any statement executed within a connection, and it defaults to the highest one supported by client libraries. Functional only with InterBase 6 and up.

role

Functional only with InterBase 5 and up.

sync

Return Values

Returns an InterBase link identifier on success, or FALSE on error.

Errors/Exceptions

If you get some error like "arithmetic exception, numeric overflow, or string truncation. Cannot transliterate character between character sets" (this occurs when you try use some character with accents) when using this and after ibase_query() you must set the character set (i.e. ISO8859_1 or your current character set).

ChangeLog

Version Description
4.0.0 The buffers, dialect and role parameters were added

Examples

Example 681. ibase_connect() example

<?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);
?>


Code Examples / Notes » ibase_connect

ag

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


fb dot bird

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!


robert silvia

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')


sam

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 :)

anthony

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


simon

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.


Łukasz wojciechowski

ag at dividi dot de wrote:
"
The solution is to insert the line
gds_db    3050/tcp    #firebird Database
into the windows services file %WINDIR%\\system32\\drivers\\etc\\services
"
...but it is very important to hit ENTER at the end of this line:
gds_db    3050/tcp    #firebird Database
I spent 2 hours before I discover this - I love Windows ;-)
--
greetings


Change Language


Follow Navioo On Twitter
ibase_add_user
ibase_affected_rows
ibase_backup
ibase_blob_add
ibase_blob_cancel
ibase_blob_close
ibase_blob_create
ibase_blob_echo
ibase_blob_get
ibase_blob_import
ibase_blob_info
ibase_blob_open
ibase_close
ibase_commit_ret
ibase_commit
ibase_connect
ibase_db_info
ibase_delete_user
ibase_drop_db
ibase_errcode
ibase_errmsg
ibase_execute
ibase_fetch_assoc
ibase_fetch_object
ibase_fetch_row
ibase_field_info
ibase_free_event_handler
ibase_free_query
ibase_free_result
ibase_gen_id
ibase_maintain_db
ibase_modify_user
ibase_name_result
ibase_num_fields
ibase_num_params
ibase_param_info
ibase_pconnect
ibase_prepare
ibase_query
ibase_restore
ibase_rollback_ret
ibase_rollback
ibase_server_info
ibase_service_attach
ibase_service_detach
ibase_set_event_handler
ibase_timefmt
ibase_trans
ibase_wait_event
eXTReMe Tracker