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//w  w w  .j ava2 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);
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions