How to Create an InterBase Database Using PHP

by Marcio Machado de Moura

PHP doesn't have a command to create an InterBase database, but we can use the isql (native interative sql client) to make this happen. For that, we only need to create a little script, and run isql with the file input parameter.

Using Isql

We will use the file input options of command line to run the script with the CREATE DATABASE command:

isql -i filename

Our objective is accept from the user the name of the database, and create a small text file that will be passed as parameter to isql.

Accepting The Database Name

The follow HTML script accepts the database name and transfers the string for a PHP script that will create and run the sql file:

<html>
<head>
<title>InterBase CREATE DATABASE</title>
</head>
<BODY bgcolor="#ffffff" text="black" link="#0000a0" alink="#0000a0" vlink="#0000a0"> <font face="arial,verdana,helvetica" size=3>
<b>InterBase CREATE DATABASE</b>
<p>
<table border=0 cellpadding=5 cellspacing=0 bgcolor="#6699cc">
<tr>
<td> <font face="arial,verdana,helvetica" size=2>
<FORM ACTION="ibcreate.php" METHOD="POST">
Database:<BR>
<INPUT NAME="ibname" TYPE=text SIZE=50 MAXSIZE=80 VALUE="">
<P>
<INPUT TYPE=submit VALUE="Send"> <INPUT VALUE="Clear" TYPE=reset>
</FORM>
</td>
</tr>
</table>
</body>
</html>

Testing If The Database Already Exists

After creating the text file , we need to check if the database already exists. The most easy method is try to connect to it.

$ibname .=".gdb"; #add .gdb extension to the database name.
$conn=ibase_connect("localhost:/home/interbase/$ibname", "SYSDBA", "masterkey");
if (!$conn) {
$ibok=true;
} else {
$ibok=false;
ibase_close($conn);
}

We will only create and run the SQL script if the boolean $ibok is true.

Creating The SQL Script file

$ibcreate="CREATE DATABASE "/home/interbase/$ibname" PASSWORD "masterkey";n";
$ibscript=fopen("/home/interbase/ibcreate.sql","w");
fwrite($ibscript,$ibcreate);
fclose($ibscript);

Note

you must have permission to write to this file.

Executing Isql

if
(!exec("/opt/interbase/bin/isql -i /home/interbase/ibcreate.sql"))
die ("Error running isql");
echo "The database: $ibname, was created with success.";

If the exec command found errors, the die ("error runing isql") will be executed, and the PHP script will be aborted.

Many other database creation parameters can also be used (e.g. PAGE SIZE, DIALECT, USER, etc.) in the DATABASE CREATE statement.

Below, the complete script - ibcreate.php:

<html>
<head>
<title>InterBase CREATE DATABASE</title>
</head>
<BODY bgcolor="#ffffff" text="black">
<font face="arial,verdana,helvetica" size=3>
<?
$ibname .=".gdb"; #add .gdb extension to the database name.
$conn=ibase_connect("localhost:/home/interbase/$ibname", "SYSDBA", "masterkey");
if (!$conn) {
   $ibok=true;
} else {
   $ibok=false;
   ibase_close($conn);
}
if ($ibok) {
$ibcreate="CREATE DATABASE "/home/interbase/$ibname" PASSWORD "masterkey";n";
$ibscript=fopen("/home/interbase/ibcreate.sql","w");
fwrite($ibscript,$ibcreate);
fclose($ibscript);

if (!exec("/opt/interbase/bin/isql -i /home/interbase/ibcreate.sql"))
die ("Error running isql");

echo "The database: $ibname, was created with success.";
} else {
echo "Can't create the database: $ibname. Database already exist.";
}
?>
</body>
</html>

You can copy and then paste this source into a HTML editor.