mysqli_affected_rows
mysqli->affected_rows
Gets the number of affected rows in a previous MySQL operation
Description
Procedural style:
mixedmysqli_affected_rows
objectlink
Object oriented style (property):
mysqli
mixedaffected_rows
mysqli_affected_rows returns the number of rows affected by the last
INSERT, UPDATE, or DELETE query associated with the provided link
parameter. If the last query was invalid, this function will return -1.
For SELECT statements mysqli_affected_rows works like
mysqli_num_rows.
The mysqli_affected_rows function only works with queries which modify
a table. In order to return the number of rows from a SELECT query, use the
mysqli_num_rows function instead.
Return Values
An integer greater than zero indicates the number of rows affected or retrieved.
Zero indicates that no records where updated for an UPDATE statement, no rows matched
the WHERE clause in the query or that no query has yet been executed.
-1 indicates that the query returned an error.
If the number of affected rows is greater than maximal int value, the number of affected rows
will be returned as a string.
Example
Example for affected rows
query("DROP TABLE IF EXISTS affected_rows");
$mysqli->query("CREATE TABLE affected_rows (a int)");
$mysqli->query("INSERT INTO affected_rows VALUES (1),(2),(3),(4)");
/* update values and retrieve number of affected rows */
$mysqli->query("UPDATE affected_rows SET a=5 WHERE a=1");
printf("Affected rows (update): %d\n", $mysqli->affected_rows);
/* delete rows and retrieve number of affected_rows */
$mysqli->query("DELETE FROM affected_rows WHERE a < 4");
printf("Affected rows (delete): %d\n", $mysqli->affected_rows);
/* select all rows and retrieve number of affected_rows */
$mysqli->query("SELECT a FROM affected_rows");
printf("Affected rows (select): %d\n", $mysqli->affected_rows);
$mysqli->close();
?>
]]>
See also
mysqli_num_rows,
mysqli_info.