Rowcount to get number of rows changed or affected by QueryWE can use rowcount() to know number of rows or records affected by the latest sql statement involving any Delete , update , insert command. This function is to be used immediately after the sql execute command. This function works similar to mysql_affected_rows function
WE will learn some examples on how to use this in our pdo example table. Download the dump file of pdo_admin table at the end of the tutorial.
Output will show number of records updated or deleted from the table ( based on which line is executed )
Same way let us try with one delete command
The output will show us number of records deleted from the pdo_admin table.
Same way you can do for insert command.
rowcount with Select CommandUsually rowcount works with delete, update and insert commands but rowCount does not give reliable data when used with MySQL so it is better to use Count command to get total number of records like this.
Here id field we used as this is an auto incremented field. If such field is not there then we can use all the records like this.
Above code returns
rowCount and truncateIf you are deleting all records of a table by using Truncate command then rowCount won't able to return you number of records deleted.
This article is written by plus2net.com team.