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 utf8mb4_unicode_ci in MySQL with PHP?
- How to join tables with PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to count the number of resulting rows in a MySQL database using PHP?
- How to use a MySQL union in PHP?
- How to set a timeout for MySQL query in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to get the last insert ID in PHP MySQL?
- How to fetch data from MySQL in PHP?
See more codes...