Challenge - 5 Problems
Binding Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of prepared statement with bound parameters
What is the output of this PHP code snippet that uses PDO to bind parameters and execute a query?
PHP
<?php $pdo = new PDO('sqlite::memory:'); $pdo->exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)'); $pdo->exec("INSERT INTO users (name) VALUES ('Alice'), ('Bob')"); $stmt = $pdo->prepare('SELECT name FROM users WHERE id = :id'); $id = 2; $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->execute(); echo $stmt->fetchColumn(); ?>
Attempts:
2 left
💡 Hint
Remember that bindParam binds the variable by reference and the value of $id is 2.
✗ Incorrect
The query selects the user with id = 2, which is 'Bob'. The bindParam binds the variable $id by reference, so the value 2 is used when executing.
❓ Predict Output
intermediate2:00remaining
Effect of changing variable after bindParam
What will this PHP code output when using bindParam and changing the variable after binding but before execution?
PHP
<?php $pdo = new PDO('sqlite::memory:'); $pdo->exec('CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT)'); $pdo->exec("INSERT INTO items (name) VALUES ('Pen'), ('Pencil')"); $stmt = $pdo->prepare('SELECT name FROM items WHERE id = :id'); $id = 1; $stmt->bindParam(':id', $id, PDO::PARAM_INT); $id = 2; $stmt->execute(); echo $stmt->fetchColumn(); ?>
Attempts:
2 left
💡 Hint
bindParam binds by reference, so the value at execution time matters.
✗ Incorrect
Because bindParam binds the variable by reference, changing $id to 2 before execute means the query uses id=2, which corresponds to 'Pencil'.
🔧 Debug
advanced2:00remaining
Why does this bindValue code not update with variable change?
Consider this PHP code using bindValue. Why does changing the variable after binding not affect the executed query?
PHP
<?php $pdo = new PDO('sqlite::memory:'); $pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT)'); $pdo->exec("INSERT INTO products (name) VALUES ('Table'), ('Chair')"); $stmt = $pdo->prepare('SELECT name FROM products WHERE id = :id'); $id = 1; $stmt->bindValue(':id', $id, PDO::PARAM_INT); $id = 2; $stmt->execute(); echo $stmt->fetchColumn(); ?>
Attempts:
2 left
💡 Hint
bindValue copies the value immediately, unlike bindParam.
✗ Incorrect
bindValue binds the value at the time of binding, so changing the variable later does not affect the query. The query uses id=1 and returns 'Table'.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this bindParam usage
Which option shows the correct way to bind a parameter using bindParam in PHP? The others contain syntax errors.
Attempts:
2 left
💡 Hint
Check for missing commas and quotes around parameter names.
✗ Incorrect
Option A correctly uses quotes around ':name' and commas between arguments. Options B, C, and D have missing commas or missing quotes causing syntax errors.
🚀 Application
expert3:00remaining
How many rows will be updated with this bindParam loop?
Given this PHP code that updates multiple rows using bindParam inside a loop, how many rows will be updated after execution?
PHP
<?php $pdo = new PDO('sqlite::memory:'); $pdo->exec('CREATE TABLE scores (id INTEGER PRIMARY KEY, score INTEGER)'); $pdo->exec("INSERT INTO scores (score) VALUES (10), (20), (30)"); $stmt = $pdo->prepare('UPDATE scores SET score = :score WHERE id = :id'); $stmt->bindParam(':score', $score, PDO::PARAM_INT); $stmt->bindParam(':id', $id, PDO::PARAM_INT); for ($i = 1; $i <= 3; $i++) { $id = $i; $score = $i * 100; $stmt->execute(); } $count = $pdo->query('SELECT COUNT(*) FROM scores WHERE score >= 100')->fetchColumn(); echo $count; ?>
Attempts:
2 left
💡 Hint
bindParam binds variables by reference, so changing them in the loop affects each execute call.
✗ Incorrect
The loop updates each row with id 1, 2, and 3, setting scores to 100, 200, and 300 respectively. All three rows have score >= 100 after the loop, so the count is 3.