Thuta Learning
PHP
AdvancedProgrammingbeginner

PDO, Prepared Statements & Transactions

PHPသင်ခန်းစာ 32

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • PDO, Prepared Statements & Transactions ရဲ့PHP runtime နဲ့web behavior ကိုရှင်းပြနိုင်ရန်
  • Normal, boundary, invalid နဲ့failure cases စမ်းနိုင်ရန်
  • Secure, maintainable PHP code ရေးနိုင်ရန်

Dependent changes အားလုံးကိုတစ်ပြိုင်နက်commit သို့rollback လုပ်ပါ။

ပိုပြီးနားလည်ထားရမယ့် အချက်

PDO prepared statements ကparameters ကိုSQL code မှခွဲပေးပါတယ်။ Multiple dependent writes/deletes ကိုtransaction ထဲလုပ်ပြီးsuccess မှcommit, failure မှrollback လုပ်ကာdriver limitations နဲ့DDL implicit commits ကိုသိပါ။

လက်တွေ့မှာ ဘယ်လိုအသုံးချမလဲ

PDO, Prepared Statements & Transactions example ကိုnormal, boundary, invalid နဲ့failure inputs ဖြင့်run/test လုပ်ပြီး output, warnings, exceptions နဲ့side effects ကိုမှတ်တမ်းတင်ပါ။

ဒီသင်ခန်းစာပြီးရင်

php
<?php
$pdo->beginTransaction();
try {
    $sql = 'UPDATE accounts SET balance = balance + :change WHERE id = :id';
    $statement = $pdo->prepare($sql);
    $statement->execute(['change' => -100, 'id' => 1]);
    $statement->execute(['change' => 100, 'id' => 2]);
    $pdo->commit();
} catch (Throwable $error) {
    if ($pdo->inTransaction()) $pdo->rollBack();
    throw $error;
}
You should see
Updates နှစ်ခုလုံးcommit မည်၊ မဟုတ်လျှင်နှစ်ခုလုံးမပြောင်းပါ။

ကိုယ်တိုင်စမ်းကြည့်ရန်

PDO, Prepared Statements & Transactions example ကိုnormal, boundary, invalid နဲ့failure inputs ဖြင့်run/test လုပ်ပြီး output, warnings, exceptions နဲ့side effects ကိုမှတ်တမ်းတင်ပါ။

Security/Common Mistake သတိပေးချက်

Exception ဖြစ်တာနဲ့transaction အလိုအလျောက်rollback မယ်ဟုမယူဆဘဲexplicit rollback လုပ်ပါ။

PDO Prepared StatementsPHP Documentation Group

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Exception ဖြစ်တာနဲ့transaction အလိုအလျောက်rollback မယ်ဟုမယူဆဘဲexplicit rollback လုပ်ပါ။
  • Sample output တစ်ခုမှန်ရုံဖြင့် input, output, authorization နဲ့failure paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

PDO, Prepared Statements & Transactions example ကိုnormal, boundary, invalid နဲ့failure inputs ဖြင့်run/test လုပ်ပြီး output, warnings, exceptions နဲ့side effects ကိုမှတ်တမ်းတင်ပါ။

You'll know it worked when: Atomic database update