PHP Oracle Database Class |
PHP Oracle Database Class ตัวอย่างนี้ผมได้ยกตัวอย่างการเขียน PHP ติดต่อกับ Oracle โดยใช้ Class เข้ามาช่วยจัดการในด้านการเพิ่ม/ลบ/แก้ไข เพื่อความสะดวกและง่ายต่อการแก้ไขหรือพัฒนาโปรแกรมครับ
PHP Oracle Config OCI8 Support
ตัวอย่าง
phpOracleFunctionClassDatabase.php
<?php
/**** Class Database ****/
Class MyDatabase
{
/**** function connect to database ****/
function MyDatabase($strUser,$strPassword,$strTns)
{
$this->objConnect = oci_connect($strUser,$strPassword,$strTns);
}
/**** function commit record ****/
function fncCommit()
{
return oci_commit($this->objConnect);
}
/**** function rollback record ****/
function fncRollBack()
{
return oci_rollback($this->objConnect);
}
/**** function insert record ****/
function fncInsertRecord()
{
$strSQL = "INSERT INTO $this->strTable ($this->strField) VALUES ($this->strValue) ";
$objParse = oci_parse($this->objConnect, $strSQL);
$objExecute = oci_execute($objParse, OCI_DEFAULT);
if($objExecute)
{
$this->fncCommit();
}
else
{
$this->fncRollBack();
}
return $objExecute;
}
/**** function select record ****/
function fncSelectRecord()
{
$strSQL = "SELECT * FROM $this->strTable WHERE $this->strCondition ";
$objParse = oci_parse($this->objConnect, $strSQL);
oci_execute($objParse, OCI_DEFAULT);
return oci_fetch_array($objParse);
}
/**** function update record (argument) ****/
function fncUpdateRecord($strTable,$strCommand,$strCondition)
{
$strSQL = "UPDATE $strTable SET $strCommand WHERE $strCondition ";
$objParse = oci_parse($this->objConnect, $strSQL);
$objExecute = oci_execute($objParse, OCI_DEFAULT);
if($objExecute)
{
$this->fncCommit();
}
else
{
$this->fncRollBack();
}
return $objExecute;
}
/**** function delete record ****/
function fncDeleteRecord()
{
$strSQL = "DELETE FROM $this->strTable WHERE $this->strCondition ";
$objParse = oci_parse($this->objConnect, $strSQL);
$objExecute = oci_execute($objParse, OCI_DEFAULT);
if($objExecute)
{
$this->fncCommit();
}
else
{
$this->fncRollBack();
}
return $objExecute;
}
/*** end class auto disconnect ***/
function __destruct() {
return oci_close($this->objConnect);
}
}
?>
phpOracleUsedFunctionClassDatabase.php
<html>
<head>
<title>ThaiCreate.Com PHP & Oracle Tutorial</title>
</head>
<body>
<?php
include("phpOracleFunctionClassDatabase.php");
//**** New class database ****//
$strTns = "TCDB";
$strUser = "myuser";
$strPassword = "mypassword";
$clsMyDB = new MyDatabase($strUser,$strPassword,$strTns);
//**** Call to class function insert record ****//
$clsMyDB->strTable = "customer";
$clsMyDB->strField = "CUSTOMERID,NAME,EMAIL,COUNTRYCODE,BUDGET,USED";
$clsMyDB->strValue = " 'C005','Weerachai Nukitram','[email protected]','TH','2000000','0' ";
$objInsert = $clsMyDB->fncInsertRecord();
if(!$objInsert)
{
echo "Record already exist.<br>";
}
else
{
echo "Record inserted.<br>";
}
echo "<br>===========================<br>";
//**** Call to function select record ****//
$clsMyDB->strTable = "customer";
$clsMyDB->strCondition = " CUSTOMERID = 'C005' ";
$objSelect = $clsMyDB->fncSelectRecord();
if(!$objSelect)
{
echo "Record not found<br>";
}
else
{
echo "Customer Detail.<br>";
echo "CUSTOMERID = $objSelect[CUSTOMERID]<br>";
echo "NAME = $objSelect[NAME]<br>";
echo "EMAIL = $objSelect[EMAIL]<br>";
echo "COUNTRYCODE = $objSelect[COUNTRYCODE]<br>";
echo "BUDGET = $objSelect[BUDGET]<br>";
echo "USED = $objSelect[USED]<br>";
}
echo "<br>===========================<br>";
//**** Call to function update record (argument) ****//
$strTable = "customer";
$strCommand = " BUDGET = '4000000' ";
$strCondition = " CUSTOMERID = 'C005' ";
$objUpdate = $clsMyDB->fncUpdateRecord($strTable,$strCommand,$strCondition);
if(!$objUpdate)
{
echo "Error update record.<br>";
}
else
{
echo "Record updated.<br>";
}
echo "<br>===========================<br>";
//**** Call to function delete record ****//
$clsMyDB->strTable = "customer";
$clsMyDB->strCondition = " CUSTOMERID = 'C005' ";
$objDelete = $clsMyDB->fncDeleteRecord();
if(!$objDelete)
{
echo "Record not delete.<br>";
}
else
{
echo "Record deleted.<br>";
}
?>
</body>
</html>
Screenshot
Oracle Server Tutorials
Reference : https://www.thaicreate.com/php-manual/ref.oci8.html
|