A transaction is a group of one or more operations performed as a single unit. It ensures that either all operations succeed together or none do, keeping data safe and consistent.
Click to reveal answer
intermediate
What are the four key properties of a transaction (ACID)?
ACID stands for: 1. Atomicity - all or nothing 2. Consistency - data stays correct 3. Isolation - transactions don’t interfere 4. Durability - changes are saved permanently
Click to reveal answer
beginner
How do you start a transaction in PHP using PDO?
Use the method beginTransaction() on your PDO object to start a transaction. Example: $pdo->beginTransaction();
Click to reveal answer
beginner
What PHP PDO methods are used to save or cancel a transaction?
To save changes, use commit(). To cancel changes, use rollBack(). Example: $pdo->commit(); or $pdo->rollBack();
Click to reveal answer
beginner
Why is transaction management important in web applications?
It keeps data safe when multiple users or processes work at the same time. It prevents errors like partial updates or lost data, making apps reliable and trustworthy.
Click to reveal answer
Which method starts a transaction in PHP PDO?
AinitTransaction()
Bstart()
CopenTransaction()
DbeginTransaction()
✗ Incorrect
The correct method to start a transaction in PHP PDO is beginTransaction().
What does the commit() method do in transaction management?
ASaves all changes made during the transaction
BStarts a new transaction
CCancels the transaction
DChecks for errors
✗ Incorrect
commit() saves all changes made during the transaction permanently.
Which ACID property ensures that transactions do not affect each other?
AAtomicity
BDurability
CIsolation
DConsistency
✗ Incorrect
Isolation ensures that transactions run independently without interfering with each other.
If an error occurs during a transaction, which method should be called to undo changes?
ArollBack()
Bcommit()
CbeginTransaction()
Dsave()
✗ Incorrect
The rollBack() method undoes all changes made during the transaction.
Why is transaction management important in multi-user applications?
ATo speed up the application
BTo prevent data conflicts and keep data consistent
CTo reduce server load
DTo allow unlimited users
✗ Incorrect
Transaction management prevents conflicts and keeps data consistent when many users access the system.
Explain the ACID properties of a transaction and why each is important.
Think about how each property helps keep data safe and reliable.
You got /5 concepts.
Describe how to use PHP PDO to manage a transaction including starting, committing, and rolling back.
Imagine you are making changes to a database and want to control when they are saved or undone.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using beginTransaction() in PHP when working with databases?
easy
A. To start a group of database operations that can be committed or rolled back together
B. To immediately save changes to the database
C. To close the database connection
D. To check if the database is available
Solution
Step 1: Understand the role of beginTransaction()
This function starts a transaction, grouping multiple database changes into one unit.
Step 2: Recognize transaction benefits
It allows committing all changes at once or rolling back if something goes wrong, ensuring data integrity.
Final Answer:
To start a group of database operations that can be committed or rolled back together -> Option A
Quick Check:
beginTransaction() starts a transaction [OK]
Hint: beginTransaction() starts safe grouped DB changes [OK]
Common Mistakes:
Thinking it saves changes immediately
Confusing it with closing the connection
Assuming it checks database availability
2. Which of the following is the correct syntax to commit a transaction in PHP PDO?
easy
A. $pdo->commitTransaction();
B. $pdo->transactionCommit();
C. commit($pdo);
D. $pdo->commit();
Solution
Step 1: Recall PDO commit method
The correct method to save all changes in a transaction is commit() called on the PDO object.
Step 2: Check method names
Only $pdo->commit(); matches the correct PDO syntax; others are invalid method names.
Final Answer:
$pdo->commit(); -> Option D
Quick Check:
commit() saves transaction changes [OK]
Hint: Use $pdo->commit() to save transaction changes [OK]
Common Mistakes:
Using incorrect method names like commitTransaction()
Trying to call commit as a function with argument
Confusing commit with rollback
3. What will be the output of the following PHP code snippet?
$pdo->beginTransaction();
$pdo->exec("INSERT INTO users (name) VALUES ('Alice')");
$pdo->rollBack();
echo $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
medium
A. 0
B. 1
C. SyntaxError
D. Throws Exception
Solution
Step 1: Analyze transaction commands
The code starts a transaction, inserts a user, then rolls back, undoing the insert.
Step 2: Check final data count
Since the insert was rolled back, the users table remains unchanged, so count is 0.
Final Answer:
0 -> Option A
Quick Check:
rollBack() undoes insert, count = 0 [OK]
Hint: rollBack() cancels changes, so count stays zero [OK]
Common Mistakes:
Assuming insert stays after rollBack()
Confusing commit() with rollBack()
Expecting syntax or runtime error
4. Identify the error in this PHP transaction code:
try {
$pdo->beginTransaction();
$pdo->exec("UPDATE accounts SET balance = balance - 100 WHERE id = 1");
$pdo->exec("UPDATE accounts SET balance = balance + 100 WHERE id = 2");
$pdo->commit();
} catch (Exception $e) {
$pdo->commit();
}
medium
A. Missing beginTransaction() call
B. Using commit() in catch block instead of rollBack()
C. No exception handling block
D. Using exec() instead of query()
Solution
Step 1: Review transaction error handling
In case of error, changes should be undone with rollBack(), not committed.
Step 2: Identify catch block mistake
The catch block incorrectly calls commit(), which saves partial or faulty changes.
Final Answer:
Using commit() in catch block instead of rollBack() -> Option B
Quick Check:
Errors need rollBack(), not commit() [OK]
Hint: Use rollBack() on error, not commit() [OK]
Common Mistakes:
Calling commit() in catch block
Forgetting to handle exceptions
Confusing exec() and query() methods
5. You want to transfer money between two accounts safely using PHP transactions. Which of the following sequences of steps is correct?
hard
A. Begin transaction, update sender balance, commit transaction, update receiver balance
B. Update sender balance, update receiver balance, begin transaction, commit transaction
C. Begin transaction, update sender balance, update receiver balance, commit transaction
D. Update sender balance, begin transaction, update receiver balance, commit transaction
Solution
Step 1: Understand transaction grouping
All related updates must be inside the transaction to ensure atomicity.
Step 2: Check step order
Starting with beginTransaction(), then updating both accounts, and finally committing ensures either all changes apply or none.
Final Answer:
Begin transaction, update sender balance, update receiver balance, commit transaction -> Option C
Quick Check:
All DB changes inside transaction before commit [OK]
Hint: Start transaction before updates, commit after all done [OK]
Common Mistakes:
Updating before starting transaction
Committing too early before all updates
Not grouping all changes inside transaction
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.