What is the purpose of using mysql_affected_rows() in PHP and when is it appropriate to use it?

The mysql_affected_rows() function in PHP is used to retrieve the number of affected rows after executing an SQL query with the MySQL database. This function can be useful when you need to know how many rows were affected by an INSERT, UPDATE, DELETE, or REPLACE query. It is appropriate to use mysql_affected_rows() after executing an SQL query to determine the impact of the query on the database.

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

// Execute an SQL query
$query = "UPDATE users SET status = 'inactive' WHERE last_login < '2022-01-01'";
mysqli_query($connection, $query);

// Get the number of affected rows
$affected_rows = mysqli_affected_rows($connection);

echo "Number of rows updated: " . $affected_rows;