ibase_connect()
So, Windows users need extension=php_interbase.dll
in their php.ini
, whereas "self-compilers" must configure PHP with the switch with-interbase=/path/to/firebird
. Then, Firebird supports two modes: a file mode comparable to SQLite and a server mode. For the sake of interoperability and for an easy deployment, this section uses the file mode.
Connecting to InterBase/Firebird
<?php if ($db = ibase_connect('localhost:/tmp/quotes.gdb', 'user', 'password')) { echo 'Connected to the database.'; ibase_close($db); } else { echo 'Connection failed.'; } ?>
This section also uses .gdb
files that are compatible with both Firebird and Interbase; the new Firebird format has the extension .fdb
. After this file is created, ibase_connect()
connects to the file or database. For the host, you have to provide a string in the format 'localhost:/path/to/file.gdb'
when using TCP/IP, or the local filename (the listings assume that the file resides in /tmp
on the local machine); you also need a username and a password.
Sending SQL to Firebird
ibase_execute()
The function ibase_query()
can be used to send an SQL string to the database. However, there is no ibase_escape_string()
; so, to be safe from SQL injection, a prepared statement must be used. Here, the function ibase_prepare()
comes into play: It parses an SQL statement (with question marks as placeholders) and returns a statement object. Then, ibase_execute()
executes this statement and retrieves the values for the placeholders as additional parameters.
Sending SQL to InterBase/Firebird
<?php if ($db = ibase_connect('localhost:/tmp/quotes.gdb', 'user', 'password')) { require_once 'stripFormSlashes.inc.php'; $sql = 'INSERT INTO quotes (id, quote, author, qyear) ' . 'VALUES (GEN_ID(quotes_gen, 1), ?, ?, ?)'; $stmt = ibase_prepare($db, $sql); ibase_execute($stmt, $_POST['quote'], $_POST['author'], intval ($_POST['year'])); echo 'Quote saved.'; ibase_close($db); } else { echo 'Connection failed.'; } ?>
The preceding code contains two specialities of Firebird. First, the identity column is driven by a generator in the database; the call to GEN_ID(quotes_gen, 1)
enters the next available value in this column when inserting a new field. Also, the word year
is reserved within Firebird, so the column's name is qyear
.