class myUDBI
{
function addDatabase($type, $name, $database, $username, $password, $portnum, $ipaddress)
{
switch($type)
{
case "ORACLE":
$tns = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS =
(PROTOCOL = TCP)(HOST = ".$ipaddress.")
(PORT = ".$portnum.")))(CONNECT_DATA =
(SERVICE_NAME = ".$database." )))";
$res = ocilogon($username,$password,$tns);
if( $res !== false )
{
$this->dbConnection[$name] = array('RES'=>$res,'TYPE'=>$type);
return true;
}
$this->dbErrorBuffer[count($this->dbErrorBuffer)] = oci_error();
break;
case "MSSQL":
$res = odbc_connect($database,$username,$password);
if( $res !== false )
{
$this->dbConnection[$name] = array('RES'=>$res,'TYPE'=>$type);
return true;
}
$this->dbErrorBuffer[count($this->dbErrorBuffer)] = odbc_errormsg();
break;
case "MYSQL":
$res = mysqli_connect($ipaddress,$username,$password,$database,$port);
if( $res !== false )
{
$this->dbConnection[$name] = array('RES'=>$res,'TYPE'=>$type);
return true;
}
$this->dbErrorBuffer[count($this->dbErrorBuffer)] = mysqli_connect_error();
break;
}
return false;
}
}
class myUDBI
{
public function closeDatabase($name = null)
{
$status = true;
$keys = array_keys($this->dbConnection);
for( $i = 0; $i < count($keys); $i++ )
{
if( $name == null || array_key_exists($name,$this->dbConnection) )
{
switch( $this->dbConnection[$keys[$i]]['TYPE'] )
{
case "ORACLE":
if( oci_close($this->dbConnection[$keys[$i]]['RES']) )
{
unset($this->dbConnection[$keys[$i]]);
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
oci_error($this->dbConnection[$keys[$i]]['RES']);
$status = false;
}
break;
case "MSSQL":
odbc_close($this->dbConnection[$keys[$i]]['RES']);
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
odbc_errormsg($this->dbErrorBuffer[$keys[$i]]['RES']);
unset($this->dbConnection[$keys[$i]]);
break;
case "MYSQL":
if( !mysqli_close($this->dbConnection[$keys[$i]]['RES']) )
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
mysqli_error($this->dbErrorBuffer[$keys[$i]]['RES']);
$status = false;
}
else
{
unset($this->dbConnection[$keys[$i]]);
}
break;
}
}
}
return $status;
}
}
class myUDBI
{
public function viewAllErrors()
{
return $this->dbErrorBuffer;
}
public function viewLastError()
{
return $this->dbErrorBuffer[count($this->dbErrorBuffer)];
}
}
class myUDBI
{
public function sendQuery($query, $name = null)
{
if( $name == null || $this->dbReplicate )
{
$keys = array_keys($this->dbConnection);
for( $i = 0; $i < count($keys); $i++ )
{
if( $name == $keys[$i] )
{
$t = $this->runQuery($query,$keys[$i]);
if( $t === false )
{
return false;
}
else
{
$retData = $t;
}
}
else
{
if( $this->runQuery($query,$keys[$i]) === false )return false;
}
}
return ( $this->dbReplicate && $retData !== true )? $retData:true;
}
else
{
return $this->runQuery($query,$name);
}
}
}
class myUDBI
{
private function runQuery($query, $name)
{
switch( $this->dbConnection[$name]['TYPE'] )
{
case "ORACLE":
$res = oci_parse($this->dbConnection[$name]['RES'],
$this->mutateQuery($query,$this->dbConnection[$name]['TYPE']));
if( $res !== false && oci_execute($res) )
{
$data = array();
while( $data[count($data)] = oci_fetch_array($res) );
return ( count($data) == 0 )? true:$data;
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
oci_error($this->dbConnection[$name]['RES']);
return false;
}
break;
case "MSSQL":
$res = odbc_prepare($this->dbConnection[$name]['RES'],
$this->mutateQuery($query,$this->dbConnection[$name]['TYPE']));
if( $res !== false && odbc_execute($res) )
{
$data = array();
while( $data[count($data)] = odbc_fetch_array($res) );
return ( count($data) == 0 )? true:$data;
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
odbc_errormsg($this->dbErrorBuffer[$name]['RES']);
return false;
}
break;
case "MYSQL":
$res = mysqli_prepare($this->dbConnection[$name]['RES'],
$this->mutateQuery($query,$this->dbConnection[$name]['TYPE']));
if( $res !== false && mysqli_execute($res) )
{
$data = array();
while( $data[count($data)] = mysqli_fetch_array($res) );
return ( count($data) == 0 )? true:$data;
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
mysqli_error($this->dbErrorBuffer[$name]['RES']);
return false;
}
break;
default:
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
"Unrecognized database type - ".$this->dbConnection[$name]['TYPE'];
return false;
}
}
}
class myUDBI
{
public function testValue($query, $name)
{
switch( $this->dbConnection[$name]['TYPE'] )
{
case "ORACLE":
$res = oci_parse($this->dbConnection[$name]['RES'],
$this->mutateQuery($query,$this->dbConnection[$name]['TYPE']));
if( $res !== false && oci_execute($res) )
{
return oci_fetch($res);
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
oci_error($this->dbConnection[$name]['RES']);
return false;
}
break;
case "MSSQL":
$res = odbc_prepare($this->dbConnection[$name]['RES'],
$this->mutateQuery($query,$this->dbConnection[$name]['TYPE']));
if( $res !== false && odbc_execute($res) )
{
return ( odbc_num_rows($res) > 0 )? true:false;
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
odbc_errormsg($this->dbErrorBuffer[$name]['RES']);
return false;
}
break;
case "MYSQL":
$res = mysqli_prepare($this->dbConnection[$name]['RES'],
$this->mutateQuery($query,$this->dbConnection[$name]['TYPE']));
if( $res !== false && mysqli_execute($res) )
{
return mysqli_stmt_fetch($res);
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
mysqli_error($this->dbErrorBuffer[$name]['RES']);
return false;
}
break;
default:
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
"Unrecognized database type - ".$this->dbConnection[$name]['TYPE'];
return false;
}
}
}
class myUDBI
{
public function testValue($query, $name)
{
switch( $this->dbConnection[$name]['TYPE'] )
{
case "ORACLE":
$res = oci_parse($this->dbConnection[$name]['RES'],
$this->mutateQuery($query,$this->dbConnection[$name]['TYPE']));
if( $res !== false && oci_execute($res) )
{
return oci_fetch($res);
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
oci_error($this->dbConnection[$name]['RES']);
return false;
}
break;
case "MSSQL":
$res = odbc_prepare($this->dbConnection[$name]['RES'],
$this->mutateQuery($query,$this->dbConnection[$name]['TYPE']));
if( $res !== false && odbc_execute($res) )
{
return ( odbc_num_rows($res) > 0 )? true:false;
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
odbc_errormsg($this->dbErrorBuffer[$name]['RES']);
return false;
}
break;
case "MYSQL":
$res = mysqli_prepare($this->dbConnection[$name]['RES'],
$this->mutateQuery($query,$this->dbConnection[$name]['TYPE']));
if( $res !== false && mysqli_execute($res) )
{
return mysqli_stmt_fetch($res);
}
else
{
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
mysqli_error($this->dbErrorBuffer[$name]['RES']);
return false;
}
break;
default:
$this->dbErrorBuffer[count($this->dbErrorBuffer)] =
"Unrecognized database type - ".$this->dbConnection[$name]['TYPE'];
return false;
}
}
}
<?php
// Database connections
$db = new myUDBI(true);
// Master database
$db->addDatabase("ORACLE",
"ORA",
"master.foo.com",
"UserA",
"abc123",
1521,
"192.168.0.10");
// Marketing database
$db->addDatabase("MYSQL",
"MRKTG",
"marketing ",
"UserB",
"abc456",
3306,
"192.168.0.20");
// Assembly database
$db->addDatabase("MSSQL",
"ASSEM",
"assembly ",
"UserC",
"abc789",
null,
"192.168.0.30");
// incoming data variables
$client = Company A’;
$expense = 1299.99;
$dateProcess = 20071201’;
// update databases
$qry = INSERT INTO TBL_CUST
(CUSTOMER,EXPNSE,ACTIONDATE)
VALUES
(‘’.$client.’´,’.$expense.’,DATE[.$dateProcess.’])’;
If( $db->sendQuery($qry) !== false )
{
echo Data Updated!’;
}
else
{
echo Error Encountered: .$db->viewLastError();
}
?>