PHP SQL Server Database Class (mssql) |
PHP SQL Server Database Class (mssql) ตัวอย่างนี้ผมได้ยกตัวอย่างการเขียน PHP ติดต่อกับ SQL Server โดยใช้ Class เข้ามาช่วยจัดการในด้านการเพิ่ม/ลบ/แก้ไข เพื่อความสะดวกและง่ายต่อการแก้ไขหรือพัฒนาโปรแกรมครับ
ตัวอย่าง
phpSQLServerFunctionClassDatabase.php
<?php
/**** Class Database ****/
Class MyDatabase
{
/**** function connect to database ****/
function MyDatabase($strHost,$strDB,$strUser,$strPassword)
{
$this->objConnect = mssql_connect($strHost,$strUser,$strPassword);
$this->DB = mssql_select_db($strDB);
}
/**** function insert record ****/
function fncInsertRecord()
{
$strSQL = "INSERT INTO $this->strTable ($this->strField) VALUES ($this->strValue) ";
return mssql_query($strSQL);
}
/**** function select record ****/
function fncSelectRecord()
{
$strSQL = "SELECT * FROM $this->strTable WHERE $this->strCondition ";
$objQuery = @mssql_query($strSQL);
return @mssql_fetch_array($objQuery);
}
/**** function update record (argument) ****/
function fncUpdateRecord($strTable,$strCommand,$strCondition)
{
$strSQL = "UPDATE $strTable SET $strCommand WHERE $strCondition ";
return @mssql_query($strSQL);
}
/**** function delete record ****/
function fncDeleteRecord()
{
$strSQL = "DELETE FROM $this->strTable WHERE $this->strCondition ";
return @mssql_query($strSQL);
}
/*** end class auto disconnect ***/
function __destruct() {
return @mssql_close($this->objConnect);
}
}
?>
phpSQLServerUsedFunctionClassDatabase.php
<html>
<head>
<title>ThaiCreate.Com PHP & SQL Server Tutorial</title>
</head>
<body>
<?php
include("phpSQLServerFunctionClassDatabase.php");
//**** New class database ****//
$strHost = "localhost";
$strDB = "mydatabase";
$strUser = "sa";
$strPassword = "";
$clsMyDB = new MyDatabase($strHost,$strDB,$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
SQL Server 2005
SQL Server 2008
Note! สำหรับ function ของ mssql
Reference : https://www.thaicreate.com/php-manual/ref.mssql.html
|