php-mysqlHow to get the number of affected rows in a MySQL using PHP?
The number of affected rows in a MySQL query can be obtained using the mysqli_affected_rows() function in PHP.
Example code
$mysqli = new mysqli("localhost", "user", "password", "database");
$query = "DELETE FROM table WHERE id = 1";
$mysqli->query($query);
$affected_rows = mysqli_affected_rows($mysqli);
echo $affected_rows;
Output example
1
Code explanation
$mysqli = new mysqli("localhost", "user", "password", "database");- creates a new mysqli object with the given parameters.$query = "DELETE FROM table WHERE id = 1";- creates a query to delete a row from the table.$mysqli->query($query);- executes the query.$affected_rows = mysqli_affected_rows($mysqli);- gets the number of affected rows from the query.echo $affected_rows;- prints the number of affected rows.
Helpful links
- mysqli_affected_rows() - PHP manual page for the
mysqli_affected_rows()function.
More of Php Mysql
- How to use a MySQL union in PHP?
- How to check if a record exists in PHP and MySQL?
- How to update to null value in MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to set a timeout for MySQL query in PHP?
- How to return multiple rows as an array in MySQL using PHP?
- How to keep a connection open in PHP and MySQL?
- How to list tables in PHP MySQL?
- How to prepare a statement in MySQL using PHP?
- How to get a single value from query in PHP MySQL?
See more codes...