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