How can the PHP function mysqli_num_rows be properly utilized in database queries?

When using the mysqli_num_rows function in PHP to determine the number of rows returned by a database query, it is important to first execute the query using mysqli_query and then pass the result set to mysqli_num_rows. This function returns the number of rows in the result set, which can be useful for various purposes such as checking if a query returned any results or displaying the total number of rows in a table.

// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Execute the query
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Check if the query returned any results
if(mysqli_num_rows($result) > 0) {
    // Process the results
    while($row = mysqli_fetch_assoc($result)) {
        // Do something with each row
    }
} else {
    echo "No results found.";
}

// Close the connection
mysqli_close($connection);