Challenge - 5 Problems
Prepared Statements Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a prepared statement execution
What will be the output of this PHP code using a prepared statement?
PHP
<?php $mysqli = new mysqli('localhost', 'user', 'pass', 'db'); $stmt = $mysqli->prepare('SELECT name FROM users WHERE id = ?'); $id = 2; $stmt->bind_param('i', $id); $stmt->execute(); $stmt->bind_result($name); $stmt->fetch(); echo $name; ?>
Attempts:
2 left
💡 Hint
Remember that bind_param binds the variable by reference and fetch() retrieves the result.
✗ Incorrect
The prepared statement selects the name where id equals 2. bind_param binds the integer 2 to the placeholder. execute() runs the query. bind_result binds the result to $name. fetch() fetches the row, so echo $name prints the user's name.
🧠 Conceptual
intermediate1:30remaining
Why use prepared statements?
Which of the following is the main reason to use prepared statements in PHP when working with databases?
Attempts:
2 left
💡 Hint
Think about security and how user input is handled.
✗ Incorrect
Prepared statements separate the SQL code from the data values, which prevents attackers from injecting malicious SQL code. This is the main security benefit.
🔧 Debug
advanced2:00remaining
Identify the error in this prepared statement code
What error will this PHP code produce when executed?
PHP
<?php $mysqli = new mysqli('localhost', 'user', 'pass', 'db'); $stmt = $mysqli->prepare('INSERT INTO users (name, age) VALUES (?, ?)'); $name = 'Alice'; $age = 'twenty'; $stmt->bind_param('si', $name, $age); $stmt->execute(); ?>
Attempts:
2 left
💡 Hint
Check the types expected by bind_param and the actual variable types.
✗ Incorrect
bind_param expects the second parameter to be an integer ('i'), but 'twenty' is a string that cannot be converted to integer, causing a TypeError or failure during execution.
📝 Syntax
advanced1:30remaining
Which prepared statement code is syntactically correct?
Which option shows a syntactically correct way to prepare and execute a statement in PHP?
Attempts:
2 left
💡 Hint
Check for missing semicolons and correct quotes around types.
✗ Incorrect
Option D has correct syntax with semicolons and quotes around 'i'. Option D misses semicolons. Option D misses quotes around 'i'. Option D misses a comma between parameters.
🚀 Application
expert2:30remaining
How many rows will be returned?
Given this PHP code using prepared statements, how many rows will be returned by the query?
PHP
<?php $mysqli = new mysqli('localhost', 'user', 'pass', 'db'); $stmt = $mysqli->prepare('SELECT * FROM users WHERE age > ?'); $age_limit = 30; $stmt->bind_param('i', $age_limit); $stmt->execute(); $result = $stmt->get_result(); echo $result->num_rows; ?>
Attempts:
2 left
💡 Hint
Check variable naming carefully.
✗ Incorrect
The variable $age_limit is missing the $ sign in assignment, so PHP treats it as a constant or undefined variable, causing a warning and $age_limit being null or zero, resulting in zero rows or error.