class CConnectionIBase {
var $err_logon = "Can't connect to database %s!";
var $descriptor = 0; // database descriptor
var $result; // result array
var $countRow = 0; // number of records in result array
var $countField = 0; // number of fields in result array
// Constructor. Creates class.
function CConnectionIBase() {}
// Opens specified database.
// Returns database descriptor.
function open($database, $username = "sysdba", $password =
"masterkey", $charset="WIN1251") {
$this->close();
$this->descriptor =
ibase_connect($database, $username, $password, $charset) or
die(sprintf($err_logon, $database));
return $this->descriptor;
}
// Clears result array. For internal use.
function freeQuery() {
unset($this->result);
$this->countRow = 0;
$this->countField = 0;
}
// Closes current connection to database
function close() {
if ($this->descriptor) {
$this->freeQuery();
ibase_close($this->descriptor);
$this->descriptor = 0;
}
}
// Executes SELECT statement and fills result array by dataset contents
// Returns number of records placed to result array
function query($code) {
$this->freeQuery();
if ($this->descriptor) {
if ($code && ($statement = ibase_query($this->descriptor, $code)))
{
while ($row = ibase_fetch_row($statement))
{
if (!$this->countRow) {
$this->countField = count($row);
for ($fno = 0; $fno < count($row); $fno++) {
$finfo = ibase_field_info($statement, $fno);
$fnames[$fno] = $finfo["name"];
$ftypes[$fno] = $finfo["type"];
}
unset($finfo);
}
for ($fno = 0; $fno < count($row); $fno++) {
if (!strcmp($ftypes[$fno], "BLOB")) {
if (($finfo = ibase_blob_info($row[$fno])) &&
($finfo["length"] > 0) &&
($blobid = ibase_blob_open($row[$fno]))) {
$blob = ibase_blob_get($blobid, $finfo["length"]);
ibase_blob_close($blobid);
} else $blob = "";
$frow[$fnames[$fno]] = $blob;
} else {
if (!is_string($row[$fno])) $frow[$fnames[$fno]] = $row[$fno];
else $frow[$fnames[$fno]] = trim($row[$fno]);
}
}
unset($finfo);
$this->result[$this->countRow++] = $frow;
}
ibase_free_result($statement);
}
}
return $this->countRow;
}
// Executes INSERT, DELETE or UPDATE statements. Result array is empty.
// Returns nonzero if all OK
function execute($code) {
$this->freeQuery();
if ($this->descriptor) {
if ($code && ($statement = ibase_query($this->descriptor,
$code))) {
ibase_free_result($statement);
return 1;
}
}
return 0;
}
// Returns content of specified cell of result array or null if $col or
$row is wrong.
// $col can to hold the field name or field index
function getData($col, $row) {
if ((0 <= $row) && ($row < $this->countRow)) {
if (is_integer($col) && (0 <= $col) && ($col <
$this->countField)) {
$row = $this->result[$row];
reset($row);
for ($fno = 0; $fno < $col; $fno++) next($row);
return current($row);
} else
if (is_string($col))
return $this->result[$row][strtoupper($col)];
} else return null;
}
// Returns field name by field index or empty string if $col is wrong
function getFieldName($col) {
if (is_integer($col) && (0 <= $col) && ($col <
$this->countField)) {
$row = $this->result[0];
reset($row);
for ($fno = 0; $fno < $col; $fno++) next($row);
list($key, $val) = each($row);
return $key;
} else return "";
}
// Commits current transaction
function commit() { ibase_commit(); }
// Rollbacks current transaction
function rollback() { ibase_rollback(); }
}
?>
Example
This example formats content of your dataset as HTML table. It's enough
to replace the bolded strings by real values.
<?
$db = new CConnectionIBase();
$db->open("YOUR_DATABASE_NAME",
"InterBase_LOGIN", "InterBase_PASSWORD");
if ($db->query("select * from YOUR_TABLE")) {
echo "<table cellPadding=2 cellSpacing=2 border=1>\n";
// Field Names
echo "<tr>\n";
for ($fno = 0; $fno < $db->countField; $fno++) {
echo "<th>";
echo $db->getFieldName($fno);
echo "</th>\n";
}
echo "</tr>\n";
// Records
for ($rno = 0; $rno < $db->countRow; $rno++) {
echo "<tr>\n";
for ($fno = 0; $fno < $db->countField; $fno++) {
echo "<td>";
echo $db->getData($fno, $rno);
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
} else echo "Dataset is empty.<br>\n";
$db->close();
?>
натяжные потолки балашиха