0
0
PHPprogramming~20 mins

Binding parameters in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binding Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
?>
AAlice
BSyntaxError
CNULL
DBob
Attempts:
2 left
💡 Hint
Remember that bindParam binds the variable by reference and the value of $id is 2.
Predict Output
intermediate
2: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();
?>
ANULL
BPencil
CPen
DFatal error
Attempts:
2 left
💡 Hint
bindParam binds by reference, so the value at execution time matters.
🔧 Debug
advanced
2: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();
?>
AbindValue copies the value at binding time, so changing $id later has no effect.
BbindValue binds by reference, so the query uses the updated $id value.
CThe code throws a runtime error because $id is changed after binding.
DThe query returns NULL because bindValue requires execute parameters.
Attempts:
2 left
💡 Hint
bindValue copies the value immediately, unlike bindParam.
📝 Syntax
advanced
2: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.
A$stmt->bindParam(':name', $name, PDO::PARAM_STR);
B$stmt->bindParam(':name' $name, PDO::PARAM_STR);
C$stmt->bindParam(:name, $name, PDO::PARAM_STR);
D$stmt->bindParam(':name', $name PDO::PARAM_STR);
Attempts:
2 left
💡 Hint
Check for missing commas and quotes around parameter names.
🚀 Application
expert
3: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;
?>
A0
BSyntaxError
C3
D1
Attempts:
2 left
💡 Hint
bindParam binds variables by reference, so changing them in the loop affects each execute call.