PHP Transactions
In this page:
try {
$pdo->beginTransaction();
// several queries
$pdo->commit(); // save all changes
} catch (Exception $e) {
$pdo->rollBack(); // undo all changes
}
एक Transaction क्या है?
एक database transaction कई SQL statements को एक single all-or-nothing unit में group करता है — या तो इसमें हर statement succeed करके save हो जाता है, या अगर कोई एक fail हो, सब roll back हो जाते हैं ऐसे जैसे कोई चला ही न हो।
उदाहरण: What is a Transaction?
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE accounts (id INTEGER, balance INTEGER)");
$db->exec("INSERT INTO accounts VALUES (1, 100), (2, 50)");
// Print "All statements in a transaction succeed together, or none do" to the output
echo "All statements in a transaction succeed together, or none do";
?>
Login to try C/C++/Java/PHP code in the editor
Changes को Commit करना
beginTransaction() (PDO) या mysqli का begin_transaction() एक transaction की शुरुआत mark करता है, जिसके बाद बाद की queries तुरंत permanently committed होने के बजाय एक pending state में रखी जाती हैं।
उदाहरण: Committing Changes
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE accounts (id INTEGER, balance INTEGER)");
$db->exec('BEGIN');
$db->exec("INSERT INTO accounts VALUES (1, 100)");
// Print "Insert is pending, not yet committed" to the output
echo "Insert is pending, not yet committed";
?>
Login to try C/C++/Java/PHP code in the editor
Changes को Rollback करना
commit() transaction शुरू होने के बाद किए गए हर change को permanently save करता है, जबकि rollback() उन्हें सब discard कर देता है — एक catch block के अंदर rollback() call करना बीच में एक failed step पर react करने का standard pattern है।
उदाहरण: Rolling Back Changes
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE accounts (id INTEGER, balance INTEGER)");
$db->exec('BEGIN');
$db->exec("INSERT INTO accounts VALUES (1, 100)");
$db->exec('ROLLBACK');
// Declare `$result`, set to `$db->query("SELECT COUNT(*) as c FROM accounts")`
$result = $db->query("SELECT COUNT(*) as c FROM accounts");
// Print a human-readable dump of `$result->fetchArray()`
print_r($result->fetchArray());
?>
Login to try C/C++/Java/PHP code in the editor
Savepoints के साथ Transactions
Transactions सबसे ज़्यादा मायने रखते हैं जब कई related writes को साथ succeed या fail करना ज़रूरी हो, जैसे दो accounts के बीच पैसे transfer करना — आप कभी नहीं चाहते कि debit succeed हो जबकि matching credit चुपचाप fail हो जाए।
उदाहरण: Transactions with Savepoints
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE accounts (id INTEGER, balance INTEGER)");
$db->exec('BEGIN');
$db->exec("UPDATE accounts SET balance = balance - 50 WHERE id = 1");
$db->exec("UPDATE accounts SET balance = balance + 50 WHERE id = 2");
$db->exec('COMMIT');
// Print "Debit and credit committed together" to the output
echo "Debit and credit committed together";
?>
Login to try C/C++/Java/PHP code in the editor
MySQLi में Transactions
database operations को एक transaction के साथ try/catch में wrap करना ensure करता है कि किसी multi-step operation के बीच में एक unexpected exception database को एक half-finished state के बजाय अपनी original, consistent state में छोड़े।
उदाहरण: Transactions in MySQLi
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE accounts (id INTEGER, balance INTEGER)");
// Try running this block; jump to `catch` if it throws
try {
$db->exec('BEGIN');
$db->exec("UPDATE accounts SET balance = balance - 50 WHERE id = 1");
$db->exec('COMMIT');
// Catch Exception $e
} catch (Exception $e) {
$db->exec('ROLLBACK');
// Print "Rolled back on failure" to the output
echo "Rolled back on failure";
}
?>
Login to try C/C++/Java/PHP code in the editor
commit()call करना भूल जाना, ताकि connection बंद होते ही changes roll back हो जाएँ।- एक failure के बाद catch block में
rollBack()call न करना, transaction को खुला छोड़ते हुए। - MyISAM जैसा एक table engine इस्तेमाल करना जो transactions support नहीं करता, ताकि rollback का कोई effect न हो।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data