0
0
PHPprogramming~20 mins

Executing queries with query method in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code using query method?

Consider the following PHP code snippet that uses the query method to fetch data from a database. What will be the output?

PHP
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'testdb');
$result = $mysqli->query('SELECT name FROM users WHERE id = 1');
$row = $result->fetch_assoc();
echo $row['name'];
?>
ASyntaxError: Unexpected token in query method
BThe name of the user with id 1 is printed
CFatal error: Call to undefined method query()
DEmpty output because query returns false
Attempts:
2 left
💡 Hint

Remember that query returns a result object on success, and you can fetch rows from it.

Predict Output
intermediate
2:00remaining
What error does this PHP code raise when using query method?

Look at this PHP code snippet. What error will it produce?

PHP
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'testdb');
$result = $mysqli->query('SELECT * FROM non_existing_table');
if (!$result) {
    echo $mysqli->error;
}
?>
AOutputs an error message about unknown table
BSyntaxError: Invalid SQL syntax
CFatal error: Call to undefined method query()
DNo output, query returns empty result
Attempts:
2 left
💡 Hint

Check what happens when the query references a table that does not exist.

🔧 Debug
advanced
2:00remaining
Why does this PHP code using query method cause a fatal error?

Examine this PHP code snippet. It causes a fatal error. What is the cause?

PHP
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'testdb');
$result = $mysqli->query('SELECT id FROM users');
echo $result['id'];
?>
Amysqli connection failed causing fatal error
BQuery method returns false due to syntax error
Cfetch_assoc() method missing causing fatal error
DTrying to access result object as array causes fatal error
Attempts:
2 left
💡 Hint

Remember what type query returns and how to access data from it.

📝 Syntax
advanced
2:00remaining
Which option correctly uses the query method to fetch all user names?

Choose the PHP code snippet that correctly fetches and prints all user names from the database using the query method.

A
&lt;?php
$result = $mysqli-&gt;query('SELECT name FROM users');
while ($row = $result-&gt;fetch_assoc()) {
    echo $row['name'] . '\n';
}
?&gt;
B
&lt;?php
$result = $mysqli-&gt;query('SELECT name FROM users');
for ($i=0; $i &lt; count($result); $i++) {
    echo $result[$i]['name'] . '\n';
}
?&gt;
C
&lt;?php
$result = $mysqli-&gt;query('SELECT name FROM users');
foreach ($result as $row) {
    echo $row['name'] . '\n';
}
?&gt;
D
&lt;?php
$result = $mysqli-&gt;query('SELECT name FROM users');
echo $result['name'];
?&gt;
Attempts:
2 left
💡 Hint

Think about how to iterate over a mysqli result set properly.

🚀 Application
expert
2:00remaining
How many rows are returned by this PHP query method call?

Given this PHP code, how many rows will $count hold after execution?

PHP
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'testdb');
$result = $mysqli->query('SELECT * FROM users WHERE age > 30');
$count = $result->num_rows;
?>
ATotal number of users in the database
BAlways zero because num_rows is not supported
CThe number of users with age greater than 30
DOne, because query returns only one row by default
Attempts:
2 left
💡 Hint

Check what num_rows property represents after a SELECT query.