nerdexam
Zend

200-710 · Question #54

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…

The correct answer is B. The INSERT will fail because of a primary key violation, and the user will see the "Success!" message. See the full explanation below for the reasoning.

Databases & SQL

Question

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!\n"; } catch (PDOException $e) { echo "Failure!\n"; 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!" message.
  • CThe INSERT will fail because of a primary key violation, and the user will see a PDO warning message.
  • DThe INSERT will fail because of a primary key violation, and the user will see the "Failure!" message.

How the community answered

(34 responses)
  • A
    3% (1)
  • B
    71% (24)
  • C
    9% (3)
  • D
    18% (6)

Topics

#PDO#primary key violation#error handling#INSERT

Community Discussion

No community discussion yet for this question.

Full 200-710 Practice