Current Location: Home> Function Categories> mysqli::$affected_rows

mysqli::$affected_rows

(mysqli_affected_rows) Get the number of rows affected in previous MySQL operations
Name:mysqli::$affected_rows
Category:MySQLi
Programming Language:php
One-line Description:Returns the number of rows affected in the last MySQL operation.

Definition and usage

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

Example

Example 1 - Object-Oriented Style

Returns the number of rows affected in different queries:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

// Execute the query and output the number of affected rows
$mysqli -> query ( "SELECT * FROM Persons" ) ;
echo "Affected rows: " . $mysqli -> affected_rows ;

$mysqli -> query ( "DELETE FROM Persons WHERE Age>32" ) ;
echo "Affected rows: " . $mysqli -> affected_rows ;

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Returns the number of rows affected in different queries:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ( ) ;
}

// Execute the query and output the number of affected rows
mysqli_query ( $con , "SELECT * FROM Persons" ) ;
echo "Affected rows: " . mysqli_affected_rows ( $con ) ;

mysqli_query ( $con , "DELETE FROM Persons WHERE Age>32" ) ;
echo "Affected rows: " . mysqli_affected_rows ( $con ) ;

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles