Hint: bindParam binds variables by reference, syntax matters [OK]
Common Mistakes:
Using bind instead of bindParam
Confusing bindParam with bindValue
Wrong method names like bindParameter
3. What will be the output of the following PHP code?
$id = 5;
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$id = 10;
$stmt->execute();
$result = $stmt->fetchAll();
print_r($result);
medium
A. Results for id = 5
B. Results for id = 10
C. Syntax error
D. Empty result
Solution
Step 1: Understand bindParam binds by reference
bindParam links the variable $id by reference, so changes to $id affect the bound value.
Step 2: Check variable change before execute
$id is changed to 10 before execute(), so the query uses id = 10.
Final Answer:
Results for id = 10 -> Option B
Quick Check:
bindParam binds by reference = Results for id = 10 [OK]
Hint: bindParam uses current variable value at execute time [OK]
Common Mistakes:
Assuming bindParam uses value at bind time
Confusing bindParam with bindValue
Expecting syntax error from this code
4. Identify the error in this PHP code snippet using PDO binding:
$stmt = $pdo->prepare('SELECT * FROM users WHERE name = :name');
$name = 'Alice';
$stmt->bindParam(':name', $name);
$name = 'Bob';
$stmt->execute();
medium
A. No error, code works correctly
B. Binding happens after changing $name variable
C. Missing PDO::PARAM_STR in bindParam call
D. Placeholder :name should be replaced manually
Solution
Step 1: Check bindParam usage
bindParam can omit the type parameter; it defaults to PDO::PARAM_STR for strings.
Step 2: Understand variable binding by reference
Changing $name before execute() updates the bound value, so 'Bob' is used.
Final Answer:
No error, code works correctly -> Option A
Quick Check:
bindParam without type is valid = No error, code works correctly [OK]
Hint: bindParam type is optional for strings [OK]
Common Mistakes:
Thinking type is mandatory in bindParam
Expecting manual placeholder replacement
Assuming binding after variable change is error
5. You want to insert multiple user records safely using PDO and binding parameters. Which approach correctly binds parameters inside a loop to insert each user?
$users = [['name' => 'Anna', 'age' => 25], ['name' => 'Ben', 'age' => 30]];
$stmt = $pdo->prepare('INSERT INTO users (name, age) VALUES (:name, :age)');
foreach ($users as $user) {
// What is the correct way to bind and execute here?
}
hard
A. Bind parameters once before loop, then change variables inside loop and execute
B. Concatenate values into SQL string inside loop and execute
C. Bind parameters inside loop using bindParam, then execute
D. Use bindValue inside loop for each value, then execute
Solution
Step 1: Understand bindParam vs bindValue in loops
bindParam binds variables by reference, which can cause issues if variables change in loop.
Step 2: Use bindValue to bind actual values each iteration
bindValue binds the current value, safe for repeated execution in loops.
Final Answer:
Use bindValue inside loop for each value, then execute -> Option D
Quick Check:
bindValue in loop = safe repeated binding [OK]
Hint: Use bindValue in loops to bind current values safely [OK]
Common Mistakes:
Binding once with bindParam and changing variables