PHP Tutorial - PHP mysqli_affected_rows() Function






Definition

The mysqli_affected_rows() function returns the number of affected rows in the previous SELECT, INSERT, UPDATE, REPLACE, or DELETE query.

Syntax

PHP mysqli_affected_rows() Function has the following syntax.

mysqli_affected_rows(connection);

Parameter

ParameterIs RequiredDescription
connectionRequired.MySQL connection

Return

It returns

  • an integer > 0 indicating the number of rows affected.
  • 0 indicates that no records where affected.
  • -1 indicates that the query returned an error.




Example

The following code prints out affected rows from different queries.


<?php/*from w  ww  . j  a v a2  s  .  c om*/
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"SELECT * FROM emp");
echo "Affected rows: " . mysqli_affected_rows($con);

mysqli_close($con);
?>