PHP Development တွင် Database Operations သည်မရှိမဖြစ်လိုအပ်သောအစိတ်အပိုင်းများဖြစ်ပြီး, ဘုံစစ်ဆင်ရေးများတွင်လည်း, သို့သော်ရိုးရာဒေတာဘေ့စ်အပြန်အလှန်ဆက်သွယ်မှုနည်းလမ်းများသည် Code Redundancy, SQL Injection အားနည်းချက်များနှင့်စွမ်းဆောင်ရည်ပြခြင်းတို့ကဲ့သို့သောပြ problems နာများရှိသည်။ ဒေတာဘေ့စ်၏အပြန်အလှန်အကျိုးသက်ရောက်မှုကိုတိုးတက်စေရန်နှင့်ကုဒ်ပြန်လည်သုံးသပ်မှုကိုတိုးမြှင့်နိုင်ရန်အတွက် MySQLI :: stmt_init method ကိုသုံးနိုင်သည်။ ဤနည်းအားဖြင့်ထိရောက်မှုကိုတိုးတက်စေနိုင်သည်သာမက SQL injection အားနည်းချက်များကိုလည်းကာကွယ်နိုင်သည်။
ဤဆောင်းပါးတွင် MySQLI :: stmt_init function ကိုသုံးရန်မည်သို့အသုံးပြုရမည်ကိုအသေးစိတ်ဖော်ပြပြီး database interaction ကိုပိုမိုထိရောက်စေရန်နှင့်ကျိုးကြောင်းဆီလျော်သော encapsulation များဖြင့်ပိုမိုထိရောက်စေရန်နှင့်လုံခြုံအောင်ပြုလုပ်ပါ။
MySQLI :: stmt_init သည် MySQLI_STMT အရာဝတ်ထုကိုအစပြုရန် MySQLI extension တွင် MySQLI extension တွင်နည်းလမ်းဖြစ်သည်။ MySQLI_STMT Object သည် SQL Queries များကိုအကောင်အထည်ဖော်သည့်အခါအထူးသဖြင့်အလားတူ SQL Queries ကိုကွပ်မျက်ရန်လိုအပ်သည့်အခါအစဉ်အလာစုံစမ်းရေးနည်းလမ်းများထက်ပိုမိုထိရောက်သော SQL ကြေငြာချက်ကိုကိုယ်စားပြုသည်။ Preprocessing ထုတ်ပြန်ချက်များသည် SQL ၏ parsing ၏အချိန်ကိုလျှော့ချနိုင်သည်။
စွမ်းဆောင်ရည်တိုးတက်မှု - preprocessing ထုတ်ပြန်ချက်များ, MySQL compille များနှင့် sql မေးမြန်းချက်များကိုတစ်ချိန်ကသူတို့အားခွဲခွာခြင်းမပြုဘဲတစ်ကြိမ်သာပြုလုပ်သည်။
SQL Injection ကိုကာကွယ်ခြင်း - ချည်နှောင်ခြင်းဆိုင်ရာ parameters တွေကိုအားဖြင့်အသုံးပြုသူထည့်သွင်းမှုအချက်အလက်များကိုတိုက်ရိုက်ထည့်သွင်းခြင်းဖြင့် SQL Injection Attack ကိုထိရောက်စွာတားဆီးနိုင်သည်။
Code Reusability : ဒေတာဘေ့စ်စစ်ဆင်ရေးအတန်းကို encapsulating ပြီးနောက် code ကိုပြန်လည်ထူထောင်ရန်နေရာအမျိုးမျိုးတွင်ပြန်လည်သုံးသပ်နိုင်သည်။
ပထမ ဦး စွာကျွန်ုပ်တို့သည်ဒေတာဘေ့စ် connection class တစ်ခုကိုဖန်တီးရန်နှင့် MySQLI :: stmt_init ကို အသုံးပြု. SQL operations များကို encapsate လိုအပ်သည်။
class Database {
private $mysqli;
public function __construct($host, $username, $password, $dbname) {
// ဒေတာဘေ့စ် connection တစ်ခုဖန်တီးပါ
$this->mysqli = new mysqli($host, $username, $password, $dbname);
// ဆက်သွယ်မှုကိုစစ်ဆေးပါ
if ($this->mysqli->connect_error) {
die("Connection failed: " . $this->mysqli->connect_error);
}
}
// ပြင်ဆင်ထား SQL ကြေညာချက်နှင့် execute
public function executeQuery($sql, $params = []) {
// စတင်ကြေငြာချက်
$stmt = $this->mysqli->stmt_init();
// ပြင်ဆင်ထား SQL အသေအချာပေြာဆိုချက်
if (!$stmt->prepare($sql)) {
die("Error in preparing statement: " . $stmt->error);
}
// parameters တွေကိုချည်ထား
if (!empty($params)) {
// parameter type ကိုရယူပါ
$types = str_repeat('s', count($params)); // အားလုံး parameters တွေကို string ကိုအမျိုးအစားများဖြစ်ကြသည်ယူဆ
$stmt->bind_param($types, ...$params);
}
// တစ် ဦး စုံစမ်းမှု execute
if ($stmt->execute()) {
// ပြန်လာရလဒ်အစုအဝေး
return $stmt->get_result();
} else {
die("Error in executing statement: " . $stmt->error);
}
}
// ဒေတာဘေ့စ် connection ကိုပိတ်ပါ
public function close() {
$this->mysqli->close();
}
}
အထက်ပါကုဒ်တွင်ဒေတာဘေ့စ်စစ်ဆင်ရေးအတန်းအစား ဒေတာဘေ့စ ်ကိုကျွန်ုပ်တို့ onsatfulate onlate onlate ons encapsulate onl ၎င်းကိုအသုံးပြုသောအခါကျွန်ုပ်တို့သည် SQL ထုတ်ပြန်ချက်များနှင့် parameters များကိုသာဖြတ်သန်းရန်သာလိုအပ်သည်။
ဥပမာအားဖြင့်အချက်အလက်ထည့်ပါ။
$db = new Database('localhost', 'root', 'password', 'm66.net');
// ဒေတာထည့်ပါ
$sql = "INSERT INTO users (name, email) VALUES (?, ?)";
$params = ['John Doe', 'johndoe@m66.net'];
$db->executeQuery($sql, $params);
အချက်အလက်ရှာဖွေမှုအချက်အလက်:
$sql = "SELECT * FROM users WHERE email = ?";
$params = ['johndoe@m66.net'];
$result = $db->executeQuery($sql, $params);
// ရလဒ်ရလဒ်
while ($row = $result->fetch_assoc()) {
echo "Name: " . $row['name'] . ", Email: " . $row['email'] . "<br>";
}
$db->close();
SQL query တစ်ခုတည်းကိုအကောင်အထည်ဖော်ရန်အပြင် Encapsulated Database စစ်ဆင်ရေးအတန်းသည်အသုတ်စစ်ဆင်ရေးများကိုလည်းအထောက်အကူပြုနိုင်သည်။ အတန်းသို့အသုတ်ခွဲစိတ်ကုသမှုနည်းလမ်းတစ်ခုထည့်ခြင်းအားဖြင့်ကျွန်ုပ်တို့သည်ဒေတာစစ်ဆင်ရေးမျိုးစုံကိုထိရောက်စွာလုပ်ဆောင်နိုင်သည်။
public function executeBatchQuery($sql, $paramsList) {
// စတင်ကြေငြာချက်
$stmt = $this->mysqli->stmt_init();
// ပြင်ဆင်ထား SQL အသေအချာပေြာဆိုချက်
if (!$stmt->prepare($sql)) {
die("Error in preparing statement: " . $stmt->error);
}
// parameter type ကိုရယူပါ
$types = str_repeat('s', count($paramsList[0])); // အားလုံး parameters တွေကို string ကိုအမျိုးအစားများဖြစ်ကြသည်ယူဆ
// parameter သည်စာရင်းနှင့် execute ကျော်ကြား
foreach ($paramsList as $params) {
$stmt->bind_param($types, ...$params);
$stmt->execute();
}
return true;
}
ဒေတာများကိုအသုတ်တွင်ထည့်ပါ။