PHP mysql_affected_rows() function
Definition and Usage
The mysql_affected_rows() function returns the number of record rows affected by the last MySQL operation.
Syntax
mysql_affected_rows(link_identifier)
Parameter | Description |
---|---|
link_identifier | Required. The MySQL connection identifier. If not specified, the last one used will be used by default. mysql_connect() Open connection. If the connection is not found, the function will try to call mysql_connect() Establish a connection and use it. If an unexpected event occurs, no connection is found or the connection cannot be established, the system emits a warning message at the E_WARNING level. |
Description
Obtain the most recent one related to link_identifier The number of record rows affected by the associated INSERT, UPDATE, or DELETE query.
Returns the value
Returns the number of affected rows if successful, or -1 if the last query fails.
If the last operation is a DELETE query without any conditions (WHERE), all records in the table will be deleted, but the return value of this function was 0 before version 4.1.2.
When using an UPDATE query, MySQL will not update the columns with the same original and new values. This makes the return value of the mysql_affected_rows() function not necessarily the number of records that match the query conditions, but only the number of truly modified records will be returned.
The REPLACE statement first deletes records with the same primary key and then inserts a new record. The function returns the sum of the number of deleted records and the number of inserted records.
Example
<?php $con = mysql_connect("localhost","mysql_user","mysql_pwd"); if (!$con) { die("Could not connect: " . mysql_error()); } mysql_select_db("mydb"); mysql_query("DELETE FROM mytable WHERE id < 5"); $rc = mysql_affected_rows();; echo "Records deleted: " . $rc; mysql_close($con); ?>
Output:
Records deleted: 4