PHP SQL Server Used Function Database Query (mssql) |
PHP SQL Server Used Function Database Query (mssql) ตัวอย่างนี้ผมได้ยกตัวอย่างการเขียน PHP ติดต่อกับ SQL Server โดยใช้ Function เข้ามาช่วยจัดการในด้านการเพิ่ม/ลบ/แก้ไข เพื่อความสะดวกและง่ายต่อการแก้ไขหรือพัฒนาโปรแกรมครับ
ตัวอย่าง
phpSQLServerFunctionDatabase.php
<?php
/**** function connection to database ****/
$objConnect = mssql_connect("localhost","sa","") or die("Error Connect to Database");
$objDB = mssql_select_db("mydatabase");
/**** function insert record ****/
function fncInsertRecord($strTable,$strField,$strValue)
{
$strSQL = "INSERT INTO $strTable ($strField) VALUES ($strValue) ";
return @mssql_query($strSQL);
}
/**** function select record ****/
function fncSelectRecord($strTable,$strCondition)
{
$strSQL = "SELECT * FROM $strTable WHERE $strCondition ";
$objQuery = @mssql_query($strSQL);
return @mssql_fetch_array($objQuery);
}
/**** function update record ****/
function fncUpdateRecord($strTable,$strCommand,$strCondition)
{
$strSQL = "UPDATE $strTable SET $strCommand WHERE $strCondition ";
return @mssql_query($strSQL);
}
/**** function delete record ****/
function fncDeleteRecord($strTable,$strCondition)
{
$strSQL = "DELETE FROM $strTable WHERE $strCondition ";
return @mssql_query($strSQL);
}
?>
phpSQLServerUsedFunctionDatabase.php
<html>
<head>
<title>ThaiCreate.Com PHP & SQL Server Tutorial</title>
</head>
<body>
<?php
include("phpSQLServerFunctionDatabase.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($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($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($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($strTable,$strCondition);
if(!$objDelete)
{
echo "Record not delete<br>";
}
else
{
echo "Record deleted.<br>";
}
mssql_close($objConnect);
?>
</body>
</html>
Screenshot
SQL Server 2005
SQL Server 2008
Note! สำหรับ function ของ mssql
Reference : https://www.thaicreate.com/php-manual/ref.mssql.html
|