200-550 · Question #213
Consider the following table data and PHP code. What is the outcome? Table data (table name "users" with primary key "id"): id name email ------- ----------- ------------------- 1 anna…
The correct answer is B. The INSERT will fail because of a primary key violation, and the user will see the "Success!". See the full explanation below for the reasoning.
Question
Consider the following table data and PHP code. What is the outcome? Table data (table name "users" with primary key "id"):
id name email ------- ----------- ------------------- 1 anna [email protected] 2 betty [email protected] 3 clara [email protected] 5 sue [email protected] PHP code (assume the PDO connection is correctly established):
$dsn = 'mysql:host=localhost;dbname=exam'; $user = 'username'; $pass = '********'; $pdo = new PDO($dsn, $user, $pass); try { $cmd = "INSERT INTO users (id, name, email) VALUES (:id, :name, :email)"; $stmt = $pdo->prepare($cmd); $stmt->bindValue('id', 1); $stmt->bindValue('name', 'anna'); $stmt->bindValue('email', '[email protected]'); $stmt->execute(); echo "Success!"; } catch (PDOException $e) { echo "Failure!"; throw $e; }
Options
- AThe INSERT will succeed and the user will see the "Success!" message.
- BThe INSERT will fail because of a primary key violation, and the user will see the "Success!"
- CThe INSERT will fail because of a primary key violation, and the user will see a PDO warning
- DThe INSERT will fail because of a primary key violation, and the user will see the "Failure!"
How the community answered
(57 responses)- A2% (1)
- B84% (48)
- C9% (5)
- D5% (3)
Community Discussion
No community discussion yet for this question.