Current Location: Home> Function Categories> mysql_affected_rows

mysql_affected_rows

Gets the number of record rows affected by the previous MySQL operation.
Name:mysql_affected_rows
Category:Uncategorized
Programming Language:php
One-line Description:Get the number of rows affected by the last MySQL operation (INSERT, UPDATE, DELETE) executed

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:

  • $link_identifier (optional): MySQL connection identifier. If not specified, the most recently opened connection is used.

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:

  1. The mysql_affected_rows() function is only applicable to MySQL connections established using mysql_connect().
  2. This function is not suitable for connecting to MySQL databases using mysqli or PDO.
  3. After PHP 5.5, the mysql_affected_rows() function has been deprecated and it is recommended to use the mysqli or PDO extension instead.
Similar Functions
Popular Articles