Installing pear MDB2 Driver for Firebird

This guide was tested on Ubuntu Hardy , I assume that php5-interbase and firebird is already installed

Next you will need to install MDB2 package using pear

$sudo su

#pear install MDB2

#pear install MDB2#ibase

Then you need to read the manual how to use this php driver

Now is time to create an mdb2 example employee.php
The connection string is created by adding driver type , in our case ibase
the username and password then the host
then comes the weird stuff if you are using linux then you separate the host and db name with an ‘/’
and then add the actual path to the file and be aware to include the ‘/’ that is why we have two slashes ‘//’
I debugged this stuff with getDebugInfo() method that gives you more info in case you get connection failed

<?php
// Create a valid MDB2 object named $mdb2
// at the beginning of your program…
require_once ‘MDB2.php’;

$options = array(
‘debug’ => 3
);

$mdb2 =& MDB2::connect(‘ibase://sysdba:masterkey@localhost//var/lib/firebird/2.1/data/employee.fdb’,$options);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage(). ‘,’ . $mdb2->getDebugInfo());
}
?>

Then add an query

// Proceed with a query…
$res =& $mdb2->query(‘SELECT * FROM employee’);

You can fetch results of a query with:

  • fetchRow() [fetch an entire row]
  • fetchOne() [fetch a single field from the row]
  • fetchAll() [fetch all rows in the result set]
  • fetchCol() [read a single column from all rows in result set]

Each one can take a fetch mode, which is one of the following constants:

  • MDB2_FETCHMODE_ORDERED [default] – An ordered array, with column numbers as keys
  • MDB2_FETCHMODE_ASSOC – an associative array (column names as keys)
  • MDB2_FETCHMODE_OBJECT – an object (column names as properties)

It’s highly recommended to use MDB2_FETCHMODE_ASSOC or MDB2_FETCHMODE_OBJECT for code clarity. You can set the fetch mode each time you call a fetch method and/or you can set the default fetch mode for the whole MDB2 instance by using the setFetchMode() method.

$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
while ($row = $res->fetchRow()) {
print $row[‘first_name’];
print”<BR>”;
}

Here is the full code finished

<?php
// Create a valid MDB2 object named $mdb2
// at the beginning of your program…
require_once ‘MDB2.php’;

$options = array(
‘debug’ => 3
);

$mdb2 =& MDB2::connect(‘ibase://sysdba:masterkey@localhost//var/lib/firebird/2.1/data/employee.fdb’,$options);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage(). ‘,’ . $mdb2->getDebugInfo());
}

// Proceed with a query…
$res =& $mdb2->query(‘SELECT * FROM employee’);

// Always check that result is not an error
if (PEAR::isError($res)) {
die($res->getMessage());
}

$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
while ($row = $res->fetchRow()) {
print $row[‘first_name’];
print”<BR>”;
}

?>

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Leave a Reply