0
0
PHPprogramming~10 mins

Transaction management in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a transaction in PHP PDO.

PHP
<?php
$pdo->[1]();
?>
Drag options to blanks, or click blank then click option'
Acommit
BbeginTransaction
Crollback
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using commit() instead of beginTransaction() to start a transaction.
Using rollback() to start a transaction.
2fill in blank
medium

Complete the code to commit a transaction in PHP PDO.

PHP
<?php
$pdo->[1]();
?>
Drag options to blanks, or click blank then click option'
Acommit
BbeginTransaction
Crollback
Dprepare
Attempts:
3 left
💡 Hint
Common Mistakes
Using rollback() instead of commit() to save changes.
Using beginTransaction() instead of commit() here.
3fill in blank
hard

Fix the error in the code to rollback a transaction in PHP PDO.

PHP
<?php
$pdo->[1]();
?>
Drag options to blanks, or click blank then click option'
Arollback
Bcommit
CbeginTransaction
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using commit() instead of rollback() to cancel changes.
Using beginTransaction() instead of rollback() here.
4fill in blank
hard

Fill both blanks to correctly handle a transaction with error catching.

PHP
<?php
try {
    $pdo->[1]();
    // some database operations
    $pdo->[2]();
} catch (Exception $e) {
    $pdo->rollback();
}
?>
Drag options to blanks, or click blank then click option'
AbeginTransaction
Bcommit
Crollback
Dprepare
Attempts:
3 left
💡 Hint
Common Mistakes
Using rollback() to save changes.
Not starting the transaction before committing.
5fill in blank
hard

Fill all three blanks to handle a transaction with error catching and rollback.

PHP
<?php
try {
    $pdo->[1]();
    $stmt = $pdo->[2]("INSERT INTO users (name) VALUES ('John')");
    $stmt->execute();
    $pdo->[3]();
} catch (Exception $e) {
    $pdo->rollback();
}
?>
Drag options to blanks, or click blank then click option'
AbeginTransaction
Bprepare
Ccommit
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using execute() instead of prepare() for the statement.
Committing before executing the statement.
Not starting the transaction before preparing.