mysqli_stmt $affected_rows Property
The mysqli_stmt::$affected_rows / mysqli_stmt_affected_rows() function returns the number of rows affected by the last SELECT, INSERT, UPDATE or DELETE query.
Syntax
//Object-oriented style $mysqli_stmt->affected_rows; //Procedural style mysqli_stmt_affected_rows(statement)
Parameters
statement |
Required. For procedural style only: Specify a mysqli_stmt object returned by mysqli_stmt_init(). |
Return Value
Returns an integer which can be interpreted as follows:
- An integer greater than zero indicates the number of rows affected or retrieved.
- Zero indicates that no records were 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 or that, for a SELECT query, this function was called prior to calling mysqli_stmt_store_result().
Note: If the number of affected rows is greater than the maximum int value (PHP_INT_MAX), the number of affected rows will be returned as a string.
Example: Object-oriented style
The example below shows the usage of mysqli_stmt::$affected_rows property.
<?php //establishing connection to the database $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_affected_rows) { echo "Failed to connect to MySQL: ". $mysqli->connect_error; exit(); } //executing select statement, storing result in //internal buffer and displaying the affected rows $stmt = $mysqli->prepare("SELECT * FROM Employee"); $stmt->execute(); $stmt->store_result(); echo "Affected rows (SELECT): ". $stmt->affected_rows; //executing delete statement and displaying the affected rows $stmt = $mysqli->prepare("DELETE FROM Employee WHERE Age>60"); $stmt->execute(); echo "Affected rows (DELETE): ". $stmt->affected_rows; //closing the connection $mysqli->close(); ?>
The output of the above code will be similar to:
Affected rows (SELECT): 478 Affected rows (DELETE): 13
Example: Procedural style
The example below shows the usage of mysqli_stmt_affected_rows() function.
<?php //establishing connection to the database $mysqli = mysqli_connect("localhost", "user", "password", "database"); if (mysqli_connect_affected_rows()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); exit(); } //executing select statement, storing result in //internal buffer and displaying the affected rows $stmt = mysqli_prepare($mysqli, "SELECT * FROM Employee"); mysqli_stmt_execute($stmt); mysqli_stmt_store_result($stmt); echo "Affected rows (SELECT): ". mysqli_stmt_affected_rows($stmt); //executing delete statement and displaying the affected rows $stmt = mysqli_prepare($mysqli, "DELETE FROM Employee WHERE Age>60"); mysqli_stmt_execute($stmt); echo "Affected rows (DELETE): ". mysqli_stmt_affected_rows($stmt); //closing the connection mysqli_close($mysqli); ?>
The output of the above code will be similar to:
Affected rows (SELECT): 478 Affected rows (DELETE): 13
❮ MySQLi Functions Reference