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 variable in a MySQL query using PHP?
- How to update to null value in MySQL using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to write an update query in MySQL using PHP?
- How to join tables with PHP and MySQL?
- How to create an SSL connection to MySQL using PHP?
- How to get a single value from query in PHP MySQL?
- How to get the version of MySQL using PHP?
- How to order by a column in ascending order in MySQL using PHP?
- How to get the error message for a MySQL query using PHP?
See more codes...