Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to prepare a SQL statement using PDO.
PHP
$stmt = $pdo->[1]("SELECT * FROM users WHERE id = :id");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using query() instead of prepare() causes errors when binding parameters.
✗ Incorrect
The prepare method is used to prepare a SQL statement for execution with bound parameters.
2fill in blank
mediumComplete the code to bind the parameter ':id' as an integer.
PHP
$stmt->bindParam(':id', $id, [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using PDO::PARAM_STR for integer parameters causes type mismatch.
✗ Incorrect
Use PDO::PARAM_INT to bind an integer parameter.
3fill in blank
hardFix the error in binding a parameter by completing the code.
PHP
$stmt->[1](':name', $name);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using bindValue instead of bindParam when variable reference is needed.
✗ Incorrect
bindParam binds a variable by reference, which is needed here.
4fill in blank
hardFill both blanks to bind ':age' as an integer and execute the statement.
PHP
$stmt->[1](':age', $age, [2]); $stmt->execute();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using bindValue instead of bindParam when variable reference is required.
Using PDO::PARAM_STR instead of PDO::PARAM_INT for integer values.
✗ Incorrect
Use bindParam with PDO::PARAM_INT to bind an integer parameter by reference.
5fill in blank
hardFill all three blanks to prepare a statement, bind ':email' as string, and execute.
PHP
$stmt = $pdo->[1]("SELECT * FROM users WHERE email = :email"); $stmt->[2](':email', $email, [3]); $stmt->execute();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using bindValue instead of bindParam when variable reference is needed.
Using PDO::PARAM_INT instead of PDO::PARAM_STR for string parameters.
✗ Incorrect
First prepare the statement, then bind the ':email' parameter as a string using bindParam and PDO::PARAM_STR, then execute.