What steps can be taken to ensure the proper handling of return values in MySQLi queries to avoid errors in PHP scripts?

When handling return values in MySQLi queries in PHP, it is important to properly check for errors and handle them accordingly to avoid issues in the script. One way to ensure proper handling is to use conditional statements to check if the query was successful before proceeding with fetching the results.

// Example of properly handling return values in MySQLi queries
$query = "SELECT * FROM users";
$result = $mysqli->query($query);

if ($result) {
    // Fetch the results
    while ($row = $result->fetch_assoc()) {
        // Process the data
    }
    // Free the result set
    $result->free();
} else {
    // Handle the error
    echo "Error: " . $mysqli->error;
}