How can the use of mysqli_affected_rows() function be beneficial in determining the success of an INSERT query in PHP, and why is it recommended over mysql_affected_rows()?

When performing an INSERT query in PHP using MySQLi, the mysqli_affected_rows() function can be beneficial in determining the success of the query by returning the number of rows affected. This can be used to verify if the INSERT operation was successful or not. It is recommended over mysql_affected_rows() because the latter is deprecated and should not be used in newer PHP versions.

// Perform an INSERT query
$sql = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')";
$result = mysqli_query($conn, $sql);

// Check if the INSERT query was successful
if (mysqli_affected_rows($conn) > 0) {
    echo "Insert query was successful.";
} else {
    echo "Insert query failed.";
}