PHP Microsoft Access Database Class (PHP ODBC) |
PHP Microsoft Access Database Class (PHP ODBC) ตัวอย่างนี้ผมได้ยกตัวอย่างการเขียน PHP ติดต่อกับ Access โดยใช้ Class เข้ามาช่วยจัดการในด้านการเพิ่ม/ลบ/แก้ไข เพื่อความสะดวกและง่ายต่อการแก้ไขหรือพัฒนาโปรแกรมครับ
DSN ODBC
ตัวอย่าง
phpMsAccessFunctionClassDatabase.php
<?php
/**** Class Database ****/
Class MyDatabase
{
/**** function connect to database ****/
function MyDatabase($strOdbc,$strUser,$strPassword)
{
$this->objConnect = odbc_connect($strOdbc,$strUser,$strPassword);
}
/**** function insert record ****/
function fncInsertRecord()
{
$strSQL = "INSERT INTO $this->strTable ($this->strField) VALUES ($this->strValue) ";
return @odbc_exec($this->objConnect, $strSQL);
}
/**** function select record ****/
function fncSelectRecord()
{
$strSQL = "SELECT * FROM $this->strTable WHERE $this->strCondition ";
$objExec = @odbc_exec($this->objConnect, $strSQL);
return @odbc_fetch_array($objExec);
}
/**** function update record (argument) ****/
function fncUpdateRecord($strTable,$strCommand,$strCondition)
{
$strSQL = "UPDATE $strTable SET $strCommand WHERE $strCondition ";
return @odbc_exec($this->objConnect, $strSQL);
}
/**** function delete record ****/
function fncDeleteRecord()
{
$strSQL = "DELETE FROM $this->strTable WHERE $this->strCondition ";
return @odbc_exec($this->objConnect, $strSQL);
}
/*** end class auto disconnect ***/
function __destruct() {
return @odbc_close($this->objConnect);
}
}
?>
phpMsAccessUsedFunctionClassDatabase.php
<html>
<head>
<title>ThaiCreate.Com PHP & Access Tutorial</title>
</head>
<body>
<?php
include("phpMsAccessFunctionClassDatabase.php");
//**** New class database ****//
$strOdbc = "mydatabase";
$strUser = "";
$strPassword = "";
$clsMyDB = new MyDatabase($strOdbc,$strUser,$strPassword);
//**** 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
Reference : https://www.thaicreate.com/php-manual/ref.uodbc.html
|