How can manual counting of affected rows in MySQL be used to track and manage image deletion operations in PHP?
To track and manage image deletion operations in PHP using manual counting of affected rows in MySQL, you can execute a DELETE query to remove the image from the database and then use the mysqli_affected_rows() function in PHP to get the number of rows affected by the query. This count can be used to confirm the deletion was successful and to update any related data or perform additional actions.
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Delete image from database
$deleteQuery = "DELETE FROM images WHERE id = 123";
$mysqli->query($deleteQuery);
// Get the number of affected rows
$affectedRows = $mysqli->affected_rows;
// Check if deletion was successful
if($affectedRows > 0) {
echo "Image deleted successfully.";
// Additional actions or updates can be performed here
} else {
echo "Failed to delete image.";
}
// Close database connection
$mysqli->close();