Работа php и firebird

Функции Firebird/InterBase

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:


ServerName www.samplehost.com
DirectoryIndex index.php index.htm
php_flag magic_quotes_sybase on

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

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.

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

/* free result */
ibase_free_query($query);
ibase_free_result($rs);

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(«
» . ibase_errmsg());

// Query
$sql=»SELECT * FROM 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 !

Here is a minimalistic code example. Be sure to create an user and a database in order to make it work.

// Minimalistic code example

// Connection
$db = ‘/path/to/database.gdb’ ;
$user = ‘username’ ;
$password = ‘password’ ;
$res = ibase_connect ( $db , $dbuser , $dbpass ) or die( «
» . 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( «
» . ibase_errmsg ());
?>

The following code can be used when creating tables in order to get auto incrementing fields:

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

Источник

Работа с Firebird в PHP через PDO

Обновлено и опубликовано

Опубликовано: 27.06.2021

Начиная с версии PHP 7.3 мы не можем использовать функции по работе с Firebird, так как они были исключены. Таким образом, можно работать с СУБД только через PDO. В данной инструкции мы рассмотрим процесс установки расширения PHP, а также примеры подключения к базе и выполнения SQL-запросов. Работа будет выполнена на примере Linux Ubuntu.

Установка расширения

Готово. Однако, если у нас несколько альтернативных версий PHP, установка должна выполняться с указанием конкретной версии, например:

Подключение к базе

  1. $dsn = ‘firebird:dbname=firebird.dmosk.ru:mydbname;charset=utf8;’;
  2. $username = ‘SYSDBA’;
  3. $password = ‘masterkey’;
  4. try
  5. $fire_conn = new PDO($dsn, $username, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
  6. > catch (PDOException $e)
  7. echo $e->getMessage();
  8. >
Оцените статью