Function name: mysqli_stmt::$affected_rows()
Applicable version: PHP 5, PHP 7
Function Description: This function is used to obtain the number of rows affected by executing the preprocessing statement.
Syntax: int mysqli_stmt::$affected_rows()
parameter:
Return value:
Example:
<?php // 假设连接数据库并执行了一条更新语句$stmt = $mysqli->prepare("UPDATE users SET name = ? WHERE id = ?"); $stmt->bind_param("si", $name, $id); $name = "John"; $id = 1; $stmt->execute(); // 获取受影响的行数$affectedRows = $stmt->affected_rows; echo "受影响的行数: " . $affectedRows; ?>
Output result:
受影响的行数: 1
In the above example, we used the $affected_rows
property of the mysqli_stmt class to get the number of rows affected after executing the update statement. First, we prepared an update statement using the prepare()
method and bound the parameters using bind_param()
method. We then execute the preprocessing statement and use the $affected_rows
property to get the number of affected rows. Finally, we output the affected number of rows to the screen.
It should be noted that the $affected_rows
attribute can only be obtained after the preprocessing statement has been executed, and can only be used in UPDATE, DELETE, and INSERT statements. For SELECT statements, you can use the $stmt->num_rows
attribute to get the number of rows in the result set.