0
0
PHPprogramming~20 mins

Transaction management in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Transaction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP transaction code?

Consider this PHP code using PDO for transaction management. What will it output?

PHP
<?php
try {
  $pdo = new PDO('sqlite::memory:');
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $pdo->exec('CREATE TABLE accounts (id INTEGER PRIMARY KEY, balance INTEGER)');
  $pdo->exec('INSERT INTO accounts (balance) VALUES (100)');
  $pdo->beginTransaction();
  $pdo->exec('UPDATE accounts SET balance = balance - 50 WHERE id = 1');
  throw new Exception('Simulated error');
  $pdo->exec('UPDATE accounts SET balance = balance + 50 WHERE id = 1');
  $pdo->commit();
} catch (Exception $e) {
  $pdo->rollBack();
  echo 'Transaction failed: ' . $e->getMessage();
}
$stmt = $pdo->query('SELECT balance FROM accounts WHERE id = 1');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo "\nBalance: " . $row['balance'];
?>
A
Transaction failed: Simulated error
Balance: 100
B
Transaction failed: Simulated error
Balance: 50
CBalance: 50
DBalance: 100
Attempts:
2 left
💡 Hint

Think about what happens when an exception is thrown inside a transaction.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes a transaction in PHP PDO?

Choose the best description of what a transaction does in PHP PDO.

AA transaction is used to speed up database queries by caching results.
BA transaction automatically commits each SQL statement separately.
CA transaction groups multiple database operations so they all succeed or fail together.
DA transaction is a PHP function that encrypts database data.
Attempts:
2 left
💡 Hint

Think about what happens if one operation in a group fails.

Predict Output
advanced
1:30remaining
What error does this PHP transaction code raise?

Look at this PHP code snippet. What error will it raise when run?

PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->beginTransaction();
$pdo->exec('CREATE TABLE test (id INTEGER)');
$pdo->commit();
$pdo->commit();
?>
APDOException: There is no active transaction
BSyntaxError: Unexpected token commit
CNo error, commits successfully twice
DFatal error: Call to undefined method commit()
Attempts:
2 left
💡 Hint

What happens if you call commit twice without a new transaction?

🔧 Debug
advanced
2:00remaining
Why does this transaction not roll back on error?

Given this PHP code, why does the balance update even though an error occurs?

PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec('CREATE TABLE accounts (id INTEGER PRIMARY KEY, balance INTEGER)');
$pdo->exec('INSERT INTO accounts (balance) VALUES (100)');
$pdo->beginTransaction();
$pdo->exec('UPDATE accounts SET balance = balance - 50 WHERE id = 1');
try {
  throw new Exception('Error happened');
  $pdo->commit();
} catch (Exception $e) {
  echo 'Error: ' . $e->getMessage();
}
$stmt = $pdo->query('SELECT balance FROM accounts WHERE id = 1');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo "\nBalance: " . $row['balance'];
?>
ABecause exceptions do not affect transactions in PDO.
BBecause rollBack() was never called, so the transaction was not undone.
CBecause commit() was called before the exception.
DBecause the database does not support transactions.
Attempts:
2 left
💡 Hint

Look for missing rollback calls in the catch block.

🚀 Application
expert
2:30remaining
How many rows are affected after this transaction code runs?

Given this PHP code, how many rows in the 'users' table will have age = 30 after it runs?

PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec('CREATE TABLE users (id INTEGER PRIMARY KEY, age INTEGER)');
$pdo->exec('INSERT INTO users (age) VALUES (25), (28), (30), (22)');
$pdo->beginTransaction();
$pdo->exec('UPDATE users SET age = 30 WHERE age < 30');
$pdo->rollBack();
$pdo->exec('UPDATE users SET age = 30 WHERE age < 30');
?>
A1
B3
C0
D4
Attempts:
2 left
💡 Hint

Consider what rollBack() does and when the second update runs.