We start off with $db
, an instance of the PDO class. After executing a query we often want to determine the number of rows that have been affected by it. The rowCount()
method of the PDOStatement
will work nicely:
$query = $db->query("DELETE FROM table WHERE name = 'John'"); $count = $query->rowCount(); echo "Deleted $count rows named John";
NOTE: This method should only be used to determine the number of rows affected by INSERT, DELETE, and UPDATE statements. Although this method may work for SELECT statements as well, it is not consistent across all databases.