PHP mysqli_affected_rows()Functions

PHP MySQLi 参考手册PHP MySQLi Reference Manual

Examples

Output the number of affected rows from different queries:

<?php
// Assume database username: root, password: 123456, database: EXAMPLE
$con=mysqli_connect("localhost","root","123456","EXAMPLE");
if (
mysqli_connect_errno($con))
{
    echo
"Connection to MySQL failed: ". mysqli_connect_error();
}

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

echo
"<br>";

mysqli_query($con,"DELETE FROM websites WHERE alexa>1000");
echo
"Number of affected rows: ". mysqli_affected_rows($con);

mysqli_close($con);
?>

Definition and Usage

The mysqli_affected_rows() function returns the number of affected rows from the previous MySQL operation (SELECT, INSERT, UPDATE, REPLACE, DELETE).


Syntax

mysqli_affected_rows(connection);

Parameters Description
connection Required. Specifies the MySQL connection to use.

Technical Details

Return value: An integer > 0 indicates the number of affected rows. 0 indicates no affected records. -1 indicates the query returned an error.
PHP Version: 5+


PHP MySQLi 参考手册PHP MySQLi Reference Manual Other Extensions