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