0
0
PHPprogramming~10 mins

Executing queries with query method in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aquery
Bexecute
Crun
Dsend
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'.
2fill in blank
medium

Complete 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'
Atrue
B0
Cfalse
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for true instead of false.
Using null which is not returned by query failures.
3fill in blank
hard

Fix 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'
Afetch_assoc
Bfetch_array
Cfetch_row
Dfetch_object
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetch_row() returns numeric indexes, not associative keys.
Using fetch_object() returns an object, not an array.
4fill in blank
hard

Fill 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'
Aquery
Bfalse
Ctrue
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using execute instead of query.
Checking for true instead of false.
5fill in blank
hard

Fill 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'
Aquery
Bfetch_assoc
C'name'
Dfetch_row
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetch_row() and trying to access by column name.
Forgetting to use quotes around the column name.