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