What are some common pitfalls when using the "affected_rows" function in mysqli?

One common pitfall when using the "affected_rows" function in mysqli is not checking for errors before calling it. It is important to first check if the query was executed successfully using the "query" function, and then check if the affected rows value is valid before using it. Additionally, ensure that the mysqli object is properly initialized and connected to the database before calling the "affected_rows" function.

// Check if the query was executed successfully
if ($result = $mysqli->query("UPDATE table SET column = 'value' WHERE condition")) {
    // Check if affected rows value is valid
    if ($mysqli->affected_rows > 0) {
        // Do something with the affected rows value
    } else {
        // No rows were affected
    }
    $result->close();
} else {
    // Query execution failed
    echo "Error: " . $mysqli->error;
}