Function name: mysql_affected_rows()
Applicable version: PHP 5.x - 5.6 (deprecated)
Usage: The mysql_affected_rows() function is used to obtain the number of rows affected by the most recent MySQL operation (INSERT, UPDATE, DELETE).
Syntax: int mysql_affected_rows ( resource $link_identifier = NULL )
parameter:
Return value: Returns the number of rows affected by the most recent MySQL operation, and if there are no affected rows, return 0. If an error occurs, return -1.
Example:
// Create a connection with the MySQL database $link = mysql_connect('localhost', 'username', 'password'); if (!$link) { die('Cannot connect to the database: ' . mysql_error()); }
// Select the database $db_selected = mysql_select_db('database_name', $link); if (!$db_selected) { die ('Cannot select database: ' . mysql_error()); }
// Execute a MySQL query $result = mysql_query("UPDATE users SET age = 30 WHERE id = 1");
if ($result) { // Get the number of affected rows $affected_rows = mysql_affected_rows($link); echo "Number of affected rows:".$affected_rows; } else { echo 'Updated data failed: ' . mysql_error(); }
// Close the database connection mysql_close($link);
Notes: