Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to execute a SQL query using the query method.
PHP
$result = $conn->[1]("SELECT * FROM users");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'execute' instead of 'query' causes an error.
Trying to call a method that doesn't exist like 'run' or 'send'.
✗ Incorrect
The query method is used to execute SQL statements in PHP's mysqli.
2fill in blank
mediumComplete the code to check if the query was successful.
PHP
if ($result === [1]) { echo "Query failed."; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for
true instead of false.Using
null which is not returned by query failures.✗ Incorrect
The query method returns false on failure.
3fill in blank
hardFix the error in the code to fetch rows from the query result.
PHP
while ($row = $result->[1]()) { echo $row['name']; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
fetch_row() returns numeric indexes, not associative keys.Using
fetch_object() returns an object, not an array.✗ Incorrect
fetch_assoc() returns an associative array, allowing access by column names.
4fill in blank
hardFill both blanks to execute a query and check if it succeeded.
PHP
$result = $conn->[1]($sql); if ($result === [2]) { echo "Error in query."; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
execute instead of query.Checking for
true instead of false.✗ Incorrect
Use query to run the SQL and check if the result is false to detect errors.
5fill in blank
hardFill all three blanks to fetch and print all user names from the query result.
PHP
$result = $conn->[1]($sql); while ($row = $result->[2]()) { echo $row[[3]] . "\n"; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
fetch_row() and trying to access by column name.Forgetting to use quotes around the column name.
✗ Incorrect
Use query to run the SQL, fetch_assoc() to get associative arrays, and access the 'name' column.